📄 server.c
字号:
MessageBox(hWnd,
"state error, timer expired and not in XFER_ACTIVE or WAITING_CONNECTION state.",
"Error",
MB_OK);
}
}
break;
case WM_DESTROY: /* message: window being destroyed */
if ((nTest_state == XFER_ACTIVE) || (nTest_state==WAITING_CONNECTION))
cleanup(hWnd);
PostQuitMessage(0);
break;
default: /* Passes it on if unproccessed */
return (DefWindowProc(hWnd, message, wParam, lParam));
}
return (NULL);
}
/**************************************************************************
FUNCTION: srv_setup(HWND)
PURPOSE: Set timer, load win_sock.dll, do a listen and set the
application state to WAITING_CONNECTION.
**************************************************************************/
VOID PASCAL srv_setup(hWnd)
HWND hWnd;
{
int nRc;
struct sockaddr_in server_addr;
/* Set a timer to get control back at set intervals. */
if (SetTimer(hWnd,1,TIMER_INTERVAL,NULL) == 0){
MessageBox(hWnd,
"Help! Can't set timer.",
"Error",
MB_OK);
return;
}
/* initialized index in screen output buffer */
nI=0;
/*
** create listen socket
*/
nListen_sd= socket( AF_INET, SOCK_STREAM, 0);
if (nListen_sd < 0) {
nTextlarr[nI]=sprintf(&szTextarr[nI][0],"socket call failed, error=%d",errno);
inc_nI(hWnd);
InvalidateRect(hWnd,NULL,TRUE);
cleanup(hWnd);
return;
}
/*
** bind to wildcard IP address and hard coded port
*/
server_addr.sin_family=AF_INET;
server_addr.sin_port=htons(SRVPORT);
server_addr.sin_addr.s_addr=INADDR_ANY;
nRc=bind( nListen_sd, (struct sockaddr far *) &server_addr,
sizeof(server_addr));
if (nRc <0) {
nTextlarr[nI]=sprintf(&szTextarr[nI][0],"bind call failed, error=%d",errno);
inc_nI(hWnd);
InvalidateRect(hWnd,NULL,TRUE);
cleanup(hWnd);
return;
}
/*
** Define listen queue with space for 1 connection
*/
nRc=listen( nListen_sd, 1);
if (nRc <0) {
nTextlarr[nI]=sprintf(&szTextarr[nI][0],"listen failed, error=%d",errno);
inc_nI(hWnd);
InvalidateRect(hWnd,NULL,TRUE);
cleanup(hWnd);
return;
}
nTest_state=WAITING_CONNECTION;
}
/**************************************************************************
FUNCTION: srv_connect(HWND)
PURPOSE: Accept the connection and set the application state to
XFER_ACTIVE.
**************************************************************************/
VOID PASCAL srv_connect(hWnd)
HWND hWnd;
{
int nRc;
int nfnds, isset_r;
fd_set fdreads;
struct timeval timeout;
/* see if connection has completed */
FD_ZERO((fd_set far *) &fdreads);
FD_SET(nListen_sd, (fd_set far *)&fdreads);
nfnds=32;
timeout.tv_sec=0;
timeout.tv_usec=0;
nRc= select(nfnds,&fdreads,NULL,NULL,&timeout);
if (nRc <0) {
nTextlarr[nI]=sprintf(&szTextarr[nI][0],"select call failed, error=%d",errno);
inc_nI(hWnd);
InvalidateRect(hWnd,NULL,TRUE);
cleanup(hWnd);
return;
}
isset_r=FD_ISSET(nListen_sd, (fd_set far *) &fdreads);
if (isset_r == 0) {
nTextlarr[nI]=sprintf(&szTextarr[nI][0],"connect not ready");
inc_nI(hWnd);
InvalidateRect(hWnd,NULL,TRUE);
return;
}
/* if connection complete do the accept */
nAcc_sd=accept( nListen_sd, NULL, NULL);
if (nAcc_sd <0) {
nTextlarr[nI]=sprintf(&szTextarr[nI][0],"Accept call failed, error=%d",errno);
inc_nI(hWnd);
InvalidateRect(hWnd,NULL,TRUE);
cleanup(hWnd);
return;
}
nTest_state=XFER_ACTIVE;
}
/****************************************************************************
FUNCTION: data_xfer(hWnd)
PURPOSE: Do a send and a receive. Check the first char of the buffer
to see when to quit.
****************************************************************************/
VOID PASCAL data_xfer(hWnd)
HWND hWnd;
{
int nTotal,nLen,nRc;
long lIoctlarg;
/* check to see if there is anything to receive */
nRc = ioctl(nAcc_sd,FIONREAD,(char * far) &lIoctlarg);
if (nRc <0) {
nTextlarr[nI]=sprintf(&szTextarr[nI][0],"ioctl call failed, 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 */
nTotal =0;
do {
nLen = recv( nAcc_sd, &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],"Recieve called failed, error=%d",errno);
inc_nI(hWnd);
InvalidateRect(hWnd,NULL,TRUE);
cleanup(hWnd);
return;
} /* if*/
if (sRecvbuff[0] ==CLOSE) {
cleanup(hWnd);
return;
}
/* Increment packet count. (Close packet is not counted.) */
nTotal_packets++;
/* send the same packet back */
nLen = send(nAcc_sd,sRecvbuff,BUFFER_SIZE,0);
if (nLen <0) {
nTextlarr[nI]=sprintf(&szTextarr[nI][0],"send call failed, error=%d",errno);
inc_nI(hWnd);
InvalidateRect(hWnd,NULL,TRUE);
cleanup(hWnd);
return;
} /* if */
nTextlarr[nI]=sprintf(&szTextarr[nI][0],"packet %d received", 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;
KillTimer(hWnd,1);
nTest_state=NOT_STARTED;
/* close sockets */
nRc= close_socket( nListen_sd);
if (nRc <0) {
nTextlarr[nI]=sprintf(&szTextarr[nI][0],"close call failed, error= %d",errno);
inc_nI(hWnd);
InvalidateRect(hWnd,NULL,TRUE);
}
nRc= close_socket(
nAcc_sd,
);
if (nRc <0) {
nTextlarr[nI]=sprintf(&szTextarr[nI][0],"closed call failed, error=%d",errno);
inc_nI(hWnd);
InvalidateRect(hWnd,NULL,TRUE);
}
nTextlarr[nI]=sprintf(&szTextarr[nI][0],"End of program. %d packets received.",nTotal_packets);
inc_nI();
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++;
nI =0;
nTextlarr[nI]=sprintf(&szTextarr[nI][0],"--wrapping around--");
nI = 1;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -