📄 client.c
字号:
lpProc = MakeProcInstance(AboutProc, hInst);
DialogBox(hInst, "AboutBox", hWnd, lpProc);
FreeProcInstance(lpProc);
break;
}
break;
case WM_TIMER: /* message: Timer message */
if (nTest_state==XFER_ACTIVE) {
data_xfer(hWnd);
}
else {
MessageBox(hWnd,
"state error, timer expired and not in XFER_ACTIVE state.",
"Error",
MB_OK);
}
break;
case WM_DESTROY: /* message: window being destroyed */
if (nTest_state==XFER_ACTIVE)
cleanup(hWnd);
PostQuitMessage(0);
break;
default: /* Passes it on if unproccessed */
return (DefWindowProc(hWnd, message, wParam, lParam));
}
return (NULL);
}
/**************************************************************************
FUNCTION: cl_setup(HWND)
PURPOSE: Sets a timer, loads win_sock.dll, establishes a connection
to the server and sends the first buffer.
**************************************************************************/
VOID PASCAL cl_setup(hWnd)
HWND hWnd;
{
int nRc, nLen, nK;
struct hostent far *hp;
struct sockaddr_in server_addr;
/* Set timer to get control back at reqular intervals. */
if (SetTimer(hWnd,1,TIMER_INTERVAL,NULL) == 0){
MessageBox(hWnd,
"Help! Can't set timer.",
"Error",
MB_OK);
return;
}
/* initialize the index to the screen output buffer */
nI=0;
/* create a socket */
nSd= socket( AF_INET, SOCK_STREAM, 0);
if (nSd < 0) {
nTextlarr[nI]=sprintf(&szTextarr[nI][0],"socket call failed, error=%d",errno);
inc_nI(hWnd);
InvalidateRect(hWnd,NULL,TRUE);
cleanup(hWnd);
return;
}
/*
** Get IP address from host file.
*/
hp = gethostbyname("win_server_node");
if (hp == NULL ) {
nTextlarr[nI]=sprintf(&szTextarr[nI][0],"win_server_node entry not in hosts file");
inc_nI(hWnd);
InvalidateRect(hWnd,NULL,TRUE);
cleanup(hWnd);
return;
}
/*
** Connect to remote socket at hard coded port.
*/
server_addr.sin_family=AF_INET;
server_addr.sin_port=htons(SRVPORT);
server_addr.sin_addr.s_addr=
*( (unsigned long far *) hp->h_addr_list[0]);
nRc=connect( nSd, (struct sockaddr far *) &server_addr,
sizeof(server_addr));
if (nRc <0) {
nTextlarr[nI]=sprintf(&szTextarr[nI][0],"Connect failed, error=%d",errno);
inc_nI(hWnd);
InvalidateRect(hWnd,NULL,TRUE);
cleanup(hWnd);
return;
}
/* initialize the packet */
for (nK=0; nK<BUFFER_SIZE; nK++)
sSendbuff[nK]=nK;
/* send first data packet */
sSendbuff[0]=DATA;
nLen = send( nSd, sSendbuff, BUFFER_SIZE, 0);
if (nLen <0) {
nTextlarr[nI]=sprintf(&szTextarr[nI][0],"error on first send, error=%d",errno);
inc_nI(hWnd);
InvalidateRect(hWnd,NULL,TRUE);
cleanup(hWnd);
return;
}
nTotal_packets++;
nTextlarr[nI]=sprintf(&szTextarr[nI][0],"packet %d sent", nTotal_packets);
inc_nI();
InvalidateRect(hWnd,NULL,TRUE);
nTest_state=XFER_ACTIVE;
}
/****************************************************************************
FUNCTION: data_xfer(hWnd)
PURPOSE: Does a receive and then a send. After a nTotal of 10 sends
and receives the cleanup routine is called.
****************************************************************************/
VOID PASCAL data_xfer(hWnd)
HWND hWnd;
{
int nLen, nRc;
int nTotal;
int nK;
long lIoctlarg;
/* check to see if there is anything to receive */
nRc = ioctl(nSd,FIONREAD,(char * far) &lIoctlarg);
if (nRc <0) {
nTextlarr[nI]=sprintf(&szTextarr[nI][0],"ioctl error=%d",errno);
cleanup(hWnd);
return;
}
if (lIoctlarg == 0) {
nTextlarr[nI]=sprintf(&szTextarr[nI][0],"Waiting to receive packet.");
inc_nI(hWnd);
InvalidateRect(hWnd,NULL,TRUE);
return;
}
/* receive packet */
nTotal =0;
do {
nLen = recv( nSd, &sRecvbuff[nTotal], BUFFER_SIZE-nTotal,
0);
if ( nLen < 0 ) {
/* break out of do while loop */
break;
}
nTotal += nLen;
} while ( nTotal < BUFFER_SIZE );
if (nLen <0) {
nTextlarr[nI]=sprintf(&szTextarr[nI][0],"Receive failed, error=%d",errno);
inc_nI(hWnd);
InvalidateRect(hWnd,NULL,TRUE);
cleanup(hWnd);
return;
} /* if*/
/* Check to see if reached NUM_SENDS total packets. */
if (nTotal_packets >= NUM_SENDS) {
cleanup(hWnd);
return;
}
/* Send the next packet. */
nLen = send(nSd,sSendbuff,BUFFER_SIZE,0);
if (nLen <0) {
nTextlarr[nI]=sprintf(&szTextarr[nI][0],"send failed, error=%d",errno);
inc_nI(hWnd);
InvalidateRect(hWnd,NULL,TRUE);
cleanup(hWnd);
return;
} /* if */
/* increment packets count */
nTotal_packets++;
nTextlarr[nI]=sprintf(&szTextarr[nI][0],"packet %d sent", nTotal_packets);
inc_nI();
InvalidateRect(hWnd,NULL,TRUE);
}
/****************************************************************************
FUNCTION: cleanup(HWND)
PURPOSE: Kills the timer, closes sockets, frees win_sock.dll
and reinitializes state variables.
****************************************************************************/
VOID PASCAL cleanup(hWnd)
HWND hWnd;
{
int nRc, nLen;
KillTimer(hWnd,1);
nTest_state=NOT_STARTED;
/* Send close packet.
* note have to send sSendbuff size packet because the server is designed
* to receive BUFFER_SIZE bytes of data and then check if it is
* a close packet.
*/
sSendbuff[0]=CLOSE;
/* send close packet */
nLen = send( nSd, sSendbuff, BUFFER_SIZE, 0);
if (nLen <0) {
nTextlarr[nI]=sprintf(&szTextarr[nI][0],"send of close packet failed, error=%d",errno);
inc_nI(hWnd);
InvalidateRect(hWnd,NULL,TRUE);
}
nTextlarr[nI]=sprintf(&szTextarr[nI][0],"End of program. %d packets sent.",nTotal_packets);
inc_nI();
InvalidateRect(hWnd,NULL,TRUE);
nRc= close_socket(
nSd,
);
if (nRc <0) {
nTextlarr[nI]=sprintf(&szTextarr[nI][0],"socket closed failed, error=%d",errno);
inc_nI(hWnd);
InvalidateRect(hWnd,NULL,TRUE);
}
nTotal_packets=0;
}
/****************************************************************************
FUNCTION: About(HWND, unsigned, WORD, LONG)
PURPOSE: Processes messages for "About" dialog box
MESSAGES:
WM_INITDIALOG - initialize dialog box
WM_COMMAND - Input received
****************************************************************************/
BOOL FAR PASCAL AboutProc(hDlg, message, wParam, lParam)
HWND hDlg;
unsigned message;
WORD wParam;
LONG lParam;
{
switch (message) {
case WM_INITDIALOG:
return (TRUE);
case WM_COMMAND:
if (wParam == IDOK
|| wParam == IDCANCEL) {
EndDialog(hDlg, TRUE);
return (TRUE);
}
return (TRUE);
}
return (FALSE);
}
/****************************************************************************
FUNCTION: inc_nI
PURPOSE: Increments the line number in the screen output buffer.
****************************************************************************/
VOID PASCAL inc_nI()
{
if (nI <MAXLINES -1) {
nI++;
}
else {
nI++;
/*InvalidateRect(hWnd,NULL,TRUE); */
nI =0;
nTextlarr[nI]=sprintf(&szTextarr[nI][0],"--wrapping around--");
nI = 1;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -