计算机网络 实验之 windows下 C++ 实现 socket 编程展示
本篇博客只是先展示一下代码,具体的编程相关请看后面的博客吧
服务器端代码 server.cpp
1 #include <stdio.h> 2 #include <winsock2.h> 3 4 #pragma comment(lib,"ws2_32.lib") 5 6 int main(int argc, char* argv[]) 7 { 8 //初始化WSA 9 WORD sockVersion = MAKEWORD(2,2); 10 WSADATA wsaData; 11 if(WSAStartup(sockVersion, &wsaData)!=0) 12 { 13 return 0; 14 } 15 16 //创建套接字 17 SOCKET slisten = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); 18 if(slisten == INVALID_SOCKET) 19 { 20 printf("socket error !"); 21 return 0; 22 } 23 24 //绑定IP和端口 25 sockaddr_in sin; 26 sin.sin_family = AF_INET; 27 sin.sin_port = htons(8888); 28 sin.sin_addr.S_un.S_addr = INADDR_ANY; 29 if(bind(slisten, (LPSOCKADDR)&sin, sizeof(sin)) == SOCKET_ERROR) 30 { 31 printf("bind error !"); 32 } 33 34 //开始监听 35 if(listen(slisten, 5) == SOCKET_ERROR) 36 { 37 printf("listen error !"); 38 return 0; 39 } 40 41 //循环接收数据 42 SOCKET sClient; 43 sockaddr_in remoteAddr; 44 int nAddrlen = sizeof(remoteAddr); 45 char revData[255]; 46 while (true) 47 { 48 printf("等待连接...\n"); 49 sClient = accept(slisten, (SOCKADDR *)&remoteAddr, &nAddrlen); 50 if(sClient == INVALID_SOCKET) 51 { 52 printf("accept error !"); 53 continue; 54 } 55 printf("接受到一个连接:%s \r\n", inet_ntoa(remoteAddr.sin_addr)); 56 57 //接收数据 58 int ret = recv(sClient, revData, 255, 0); 59 if(ret > 0) 60 { 61 revData[ret] = 0x00; 62 printf(revData); 63 } 64 65 //发送数据 66 const char * sendData = "你好,TCP客户端!\n"; 67 send(sClient, sendData, strlen(sendData), 0); 68 closesocket(sClient); 69 } 70 71 closesocket(slisten); 72 WSACleanup(); 73 return 0; 74 }
客户端代码 client.cpp:
1 #include<WINSOCK2.H> 2 #include<STDIO.H> 3 #include<iostream> 4 #include<cstring> 5 using namespace std; 6 #pragma comment(lib, "ws2_32.lib") 7 8 int main() 9 { 10 WORD sockVersion = MAKEWORD(2, 2); 11 WSADATA data; 12 if(WSAStartup(sockVersion, &data)!=0) 13 { 14 return 0; 15 } 16 while(true){ 17 SOCKET sclient = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); 18 if(sclient == INVALID_SOCKET) 19 { 20 printf("invalid socket!"); 21 return 0; 22 } 23 24 sockaddr_in serAddr; 25 serAddr.sin_family = AF_INET; 26 serAddr.sin_port = htons(8888); 27 serAddr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1"); 28 if(connect(sclient, (sockaddr *)&serAddr, sizeof(serAddr)) == SOCKET_ERROR) 29 { //连接失败 30 printf("connect error !"); 31 closesocket(sclient); 32 return 0; 33 } 34 35 string data; 36 cin>>data; 37 const char * sendData; 38 sendData = data.c_str(); //string转const char* 39 //char * sendData = "你好,TCP服务端,我是客户端\n"; 40 send(sclient, sendData, strlen(sendData), 0); 41 //send()用来将数据由指定的socket传给对方主机 42 //int send(int s, const void * msg, int len, unsigned int flags) 43 //s为已建立好连接的socket,msg指向数据内容,len则为数据长度,参数flags一般设0 44 //成功则返回实际传送出去的字符数,失败返回-1,错误原因存于error 45 46 char recData[255]; 47 int ret = recv(sclient, recData, 255, 0); 48 if(ret>0){ 49 recData[ret] = 0x00; 50 printf(recData); 51 } 52 closesocket(sclient); 53 } 54 55 56 WSACleanup(); 57 return 0; 58 59 }
在Codeblocks中如果不进行一些措施可能报以下错误:
解决方法:
右键相关project:
去codeblocks相关文件夹下找到此文件,添加就好了:
如何同时运行两个程序呢?
1.找到你所建的项目的文件夹:
这就ok啦~~~