计算机网络实验指导书 下载本文

nPort = atoi(argv[1]);//将端口号由“字符串”格式转换成“短整数”格式 //

//初始化通信接口winsocket

nRet = WSAStartup(wVersionRequested, &wsaData); if (wsaData.wVersion != wVersionRequested) { fprintf(stderr,\ return; } //

// Do the stuff a stream server does //

StreamServer(nPort); //

// Release WinSock

///去初始化通信接口winsocket

WSACleanup(); }

////////////////////////////////////////////////////////////

void StreamServer(short nPort) { // // Create a TCP/IP stream socket to \ SOCKET listenSocket;//定义侦听套接字 listenSocket = socket(AF_INET, // Address family SOCK_STREAM, // Socket type IPPROTO_TCP); // Protocol if (listenSocket == INVALID_SOCKET) { PRINTERROR(\ return; } // // Fill in the address structure SOCKADDR_IN saServer; //初始化本地套接字地址 saServer.sin_family = AF_INET; saServer.sin_addr.s_addr = INADDR_ANY; // Let WinSock supply address saServer.sin_port = htons(nPort); // Use port from command line //

41

// bind the name to the socket // int nRet; nRet = bind(listenSocket, // Socket (LPSOCKADDR)&saServer, // Our address sizeof(struct sockaddr)); // Size of address structure if (nRet == SOCKET_ERROR) { PRINTERROR(\ closesocket(listenSocket); return; } // // This isn't normally done or required, but in this // example we're printing out where the server is waiting // so that you can connect the example client. // int nLen; nLen = sizeof(SOCKADDR); char szBuf[256]; nRet = gethostname(szBuf, sizeof(szBuf)); //获取本地主机名 if (nRet == SOCKET_ERROR) { PRINTERROR(\ closesocket(listenSocket); return; } // Show the server name and port number printf(\ szBuf, nPort); // Set the socket to listen printf(\

//在套接字listenSocket上进行侦听(来自客户端的连接请求) nRet = listen(listenSocket, // Bound socket SOMAXCONN); // Number of connection request queue if (nRet == SOCKET_ERROR) { PRINTERROR(\ closesocket(listenSocket);

42

}

return;

//

// Wait for an incoming request //

SOCKET remoteSocket;

printf(\

remoteSocket = accept(listenSocket, // Listening socket NULL, // Optional client address NULL); //创建一个新的套接字,实现与客户端的tcp连接 if (remoteSocket == INVALID_SOCKET) { PRINTERROR(\ closesocket(listenSocket); return; }

//

// We're connected to a client

// New socket descriptor returned already // has clients address

//

// Receive data from the client //

memset(szBuf, 0, sizeof(szBuf)); //将接收缓冲区“清零” nRet = recv(remoteSocket, // Connected client szBuf, // Receive buffer sizeof(szBuf), // Lenght of buffer 0); // Flags if (nRet == INVALID_SOCKET) { PRINTERROR(\ closesocket(listenSocket); closesocket(remoteSocket); return; }

//

// Display received data //

printf(\

43

}

//

// Send data back to the client //

strcpy(szBuf, \nRet = send(remoteSocket, szBuf, strlen(szBuf)+1, 0);

// Connected socket // Data buffer // Lenght of data // Flags

//

// Close BOTH sockets before exiting //

closesocket(remoteSocket);//关闭套接字 closesocket(listenSocket); //关闭套接字 return;

(二)客户端程序:client.cpp

// Compile and link with wsock32.lib

// Pass the server name and port number on the command line. // Example: Client MyMachineName 2000 //

#include #include

#pragma comment(lib,\

// Function prototype

void StreamClient(char *szServer, short nPort);

// Helper macro for displaying errors #define PRINTERROR(s) \\ fprintf(stderr,\

////////////////////////////////////////////////////////////

void main(int argc, char **argv) {

WORD wVersionRequested = MAKEWORD(1,1); WSADATA wsaData; int nRet; short nPort;

44