1 #include "stdafx.h" 2 3 #define BUF_SIZE 64 4 #include "winsock2.h" 5 #include <iostream> 6 using namespace std; 7 #pragma comment(lib,"ws2_32.lib") 8 9 int main() 10 { 11 WSADATA wsd; //WSADATA变量 12 SOCKET sServer; //服务器套接字 13 SOCKET sClient; //客户端套接字 14 SOCKADDR_IN addServ; //服务器地址 15 char buf[BUF_SIZE]; //接收数据缓冲区 16 int retVal; //返回值 17 18 //初始化套接字动态库 19 if(WSAStartup(MAKEWORD(2,2), &wsd) != 0) 20 { 21 printf("初始化套接字动态库失败了"); 22 return 1; 23 } 24 25 //创建套接字 26 sServer = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP); 27 if(INVALID_SOCKET == sServer) 28 { 29 printf("创建套接字失败"); 30 WSACleanup(); //释放资源 31 return -1; 32 } 33 34 //弄一个服务器套接字地址 35 addServ.sin_family = AF_INET; 36 addServ.sin_port = htons(4999); 37 addServ.sin_addr.s_addr = INADDR_ANY; 38 39 //绑定套接字 40 retVal = bind(sServer,(LPSOCKADDR)&addServ,sizeof(SOCKADDR_IN)); 41 if(SOCKET_ERROR == retVal) 42 43 { 44 printf("绑定套接字失败"); 45 closesocket(sServer); 46 WSACleanup(); 47 return -1; 48 } 49 else 50 { 51 cout<<"server succeeded!"<<endl; 52 } 53 54 //开始监听 55 retVal = listen(sServer,1); 56 if(retVal == SOCKET_ERROR) 57 { 58 printf("监听失败"); 59 closesocket(sServer); 60 WSACleanup(); 61 return -1; 62 } 63 else 64 { 65 cout<<"waiting for client ...."<<endl; 66 } 67 68 //接受客户端请求 69 sockaddr_in addClient; 70 int addrClientlen = sizeof(addClient); 71 sClient = accept(sServer,(sockaddr FAR*)&addClient,&addrClientlen); 72 if(sClient == INVALID_SOCKET) 73 { 74 printf("接受客户端请求失败"); 75 closesocket(sServer); 76 77 WSACleanup(); 78 return -1; 79 } 80 else 81 { 82 cout<<"Accept a client"<<endl; 83 } 84 85 //接受客户端数据 86 ZeroMemory(buf,BUF_SIZE); 87 retVal = recv(sClient,buf,BUF_SIZE,0); 88 if(retVal == SOCKET_ERROR) 89 { 90 printf("接受客户端数据时不幸"); 91 closesocket(sServer); 92 closesocket(sClient); 93 WSACleanup(); 94 return -1; 95 } 96 //printf("%s",buf); 97 cout<<buf<<endl; 98 99 100 101 //退出 102 closesocket(sServer); 103 closesocket(sClient); 104 WSACleanup(); 105 cout<<"server exiting..."<<endl; 106 system("pause"); 107 return 0; 108 109 110 111 }
1 // client.cpp : 定义控制台应用程序的入口点。 2 // 3 4 #include "stdafx.h" 5 #include "winsock2.h" 6 #include <iostream> 7 using namespace std; 8 #pragma comment(lib,"ws2_32.lib") 9 #define BUF_SIZE 64 10 11 int _tmain(int argc, _TCHAR* argv[]) 12 { 13 WSADATA wsd; 14 SOCKET sHost; //服务器套接字 15 SOCKADDR_IN servAddr; //服务器地址 16 char buf[BUF_SIZE]; //接受数据缓冲区 17 int retVal; //返回值 18 19 //初始化套接字动态库 20 if(WSAStartup(MAKEWORD(2,2),&wsd) != 0 ) 21 { 22 printf("WSAStartup failed\n"); 23 return -1; 24 } 25 else 26 { 27 cout<<"client succeeded!"<<endl; 28 } 29 30 //创建套接字 31 sHost = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP); 32 if(sHost ==INVALID_SOCKET) 33 { 34 printf("socket failed"); 35 WSACleanup(); 36 return -1; 37 } 38 39 //配置服务器套接字地址还有端口什么的 40 servAddr.sin_family = AF_INET; 41 servAddr.sin_addr.s_addr = inet_addr("127.0.0.1"); 42 servAddr.sin_port = htons(4999); 43 int nServAddlen = sizeof(servAddr); 44 cout<<"be ready to connect to server"<<endl; 45 46 //链接服务器地址 47 retVal = connect(sHost,(LPSOCKADDR)&servAddr,sizeof(servAddr)); 48 if(retVal == SOCKET_ERROR) 49 { 50 printf("connect falied"); 51 system("pause"); 52 closesocket(sHost); 53 WSACleanup(); 54 return -1; 55 } 56 else 57 { 58 cout<<"connceted successfully"<<endl; 59 } 60 61 //向服务器发送数据 62 ZeroMemory(buf,BUF_SIZE); 63 strcpy(buf,"Hello Server"); 64 65 retVal = send(sHost,buf,strlen(buf),0); 66 if(retVal == SOCKET_ERROR) 67 { 68 printf("send failed"); 69 closesocket(sHost); 70 WSACleanup(); 71 return -1; 72 } 73 74 //退出 75 closesocket(sHost); 76 WSACleanup(); 77 cout<<"client exiting..."<<endl; 78 system("pause"); 79 return 0; 80 81 }
现在想在服务端里打印出客户端的端口号,和客户端的ip地址。
应该用什么方法?
getpeername 好像是这个。