socket编程
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了socket编程相关的知识,希望对你有一定的参考价值。
sicket编写了聊天小程序和猜数字大小小游戏,发现了一些问题,send函数参数中的字符串大小要用strlen()来计算 而recv()要用sizeof()来计算或者指定大小,否则在多次接收发送中会出现问题只有第一次是正常的发送接收。
#pragma comment(lib,"ws2_32.lib") #include<stdio.h> #include<stdlib.h> #include<winsock2.h> #include<time.h> void main() { WSADATA wsaData; SOCKET ListeningSocket; SOCKET NewConnection; SOCKADDR_IN ServerAddr; SOCKADDR_IN ClientAddr; char DataBuffer[1024]; char SendBuffer[1024]; int ClientAddrLen; int Port = 5151; int Ret; if((WSAStartup(MAKEWORD(2,2),&wsaData)) != 0) //通过WSAStarup()函数启动winsoock 2 API 参数为版本号和启用的WSADATA对象 { printf("WSAStartup failed with erroe %d\n",Ret); return; } if((ListeningSocket = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP)) == INVALID_SOCKET) //创建套接字socket() 参数为地址簇为AF_INET 流SOCK_STREAM,和 IP--- 代表TCP连接 { printf("socket failed with error &d\n",WSAGetLastError()); WSACleanup(); return; } ServerAddr.sin_family = AF_INET; //协议 ServerAddr.sin_port = htons(Port); //要监听的端口号 ServerAddr.sin_addr.S_un.S_addr = htonl(INADDR_ANY); //服务器端监听所有的IP地址 if(bind(ListeningSocket,(SOCKADDR*)&ServerAddr,sizeof(ServerAddr)) == SOCKET_ERROR) //把套接字和服务端地址绑定 { printf("bind failed with error %d\n",WSAGetLastError()); closesocket(ListeningSocket); WSACleanup(); return; } if(listen(ListeningSocket,5) == SOCKET_ERROR) //利用服务端套接字进行监听 5是最大排队等待连接数 { printf("listen failed with error %d\n",WSAGetLastError()); closesocket(ListeningSocket); WSACleanup(); return; } ClientAddrLen = sizeof(SOCKADDR); printf("等待客户端连接.......\n"); if((NewConnection = accept(ListeningSocket,(SOCKADDR*)&ClientAddr,&ClientAddrLen)) == INVALID_SOCKET) //接收客户端的连接请求 { printf("accept failed with error %d\n",WSAGetLastError); closesocket(ListeningSocket); WSACleanup(); return; } int i,j,n=0; char num[1024]; srand((unsigned)time(NULL)); i = rand()%300; printf("产生的随机数为:%d\n",i); itoa(i,num,10); if((Ret = send(NewConnection,num,strlen(num),0)) == SOCKET_ERROR) { printf("send failed with error%d\n",WSAGetLastError()); closesocket(NewConnection); WSACleanup(); return; } while(1) { if((Ret = recv(NewConnection,DataBuffer,sizeof(DataBuffer),0))== SOCKET_ERROR) { printf("recv failed with error %d\n",WSAGetLastError()); closesocket(NewConnection); WSACleanup(); return; } //printf("接收到的字符串%s\n",DataBuffer); n++; printf("第%d次猜测\n",n); DataBuffer[Ret] = ‘\0‘; j = atoi(DataBuffer); printf("接受到的值%d\n",j); if(j > i) { SendBuffer[0] = ‘1‘; SendBuffer[1] = ‘\0‘; if((Ret = send(NewConnection,SendBuffer,strlen(SendBuffer),0)) == SOCKET_ERROR) { printf("send failed with error%d\n",WSAGetLastError()); closesocket(NewConnection); WSACleanup(); return; } } else if(j < i) { SendBuffer[0] = ‘2‘; SendBuffer[1] = ‘\0‘; if((Ret = send(NewConnection,SendBuffer,strlen(SendBuffer),0)) == SOCKET_ERROR) { printf("send failed with error%d\n",WSAGetLastError()); closesocket(NewConnection); WSACleanup(); return; } } else if(j == i) { SendBuffer[0] = ‘3‘; SendBuffer[1] = ‘\0‘; if((Ret = send(NewConnection,SendBuffer,strlen(SendBuffer),0)) == SOCKET_ERROR) { printf("send failed with error%d\n",WSAGetLastError()); closesocket(NewConnection); WSACleanup(); return; } break; } } closesocket(ListeningSocket); closesocket(NewConnection); WSACleanup(); }
#pragma comment(lib,"ws2_32.lib") #include<stdio.h> #include<stdlib.h> #include<time.h> #include<winsock2.h> //void main(int argc,char **argv) //argc表示后面有几个参数,argv是值是字符数组,顺序与输出时不一一对应 这样需要用CMD到目录去打开!!! void main() { WSADATA wsaData; SOCKET s; SOCKADDR_IN ServerAddr; int Ret; int Port=5151; char sendBuffer[1024]; char tempBuffer[1024]; //if(argc <= 1) //{ // printf("USAGE: TCPClient <Server IP address>.\n"); // return; //} if((Ret = WSAStartup(MAKEWORD(2,2),&wsaData)) != 0) { printf("WSAStartup failed with error %d\n",Ret); return; } if((s = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP)) ==INVALID_SOCKET) { printf("socket failed with error %d\n",Ret); return; } ServerAddr.sin_family = AF_INET; ServerAddr.sin_port = htons(Port); //ServerAddr.sin_addr.S_un.S_addr = inet_addr(argv[1]); ServerAddr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1"); if(connect(s,(SOCKADDR*)&ServerAddr,sizeof(ServerAddr)) == SOCKET_ERROR) { printf("connect failed with error %d\n",WSAGetLastError()); closesocket(s); WSACleanup(); return; } printf("连接服务器成功!\n"); printf("猜数字小游戏:服务器已经生成了一个数字,猜一猜是多少?\n"); char num[1024]; int a; if((Ret = recv(s,num,sizeof(num),0)) == SOCKET_ERROR) { printf("recv failed with error %d\ne",WSAGetLastError()); closesocket(s); WSACleanup(); return; } a = atoi(num); while(1) { int i; printf("请输入你猜测的数:\n"); gets(sendBuffer); if((Ret = send(s,sendBuffer,strlen(sendBuffer),0)) == SOCKET_ERROR) { printf("send failed with error%d\n",WSAGetLastError()); closesocket(s); WSACleanup(); return; } if((Ret = recv(s,tempBuffer,sizeof(tempBuffer),0)) == SOCKET_ERROR) { printf("recv failed with error %d\ne",WSAGetLastError()); closesocket(s); WSACleanup(); return; } tempBuffer[Ret] = ‘\0‘; //printf("接收的字符串%s\n",tempBuffer); //i = atoi(tempBuffer); //printf("接受到的值%s\n",i); if(tempBuffer[0] == ‘1‘) { printf("你猜的数太大了,换一个小的试试!\n"); } else if(tempBuffer[0] == ‘2‘) { printf("你猜的数太小了,换一个大的试试!\n"); } else if(tempBuffer[0] == ‘3‘) { printf("恭喜你猜对了,最终答案是:%d\n",a);break; } ZeroMemory(tempBuffer,1024); ZeroMemory(sendBuffer,1024); } closesocket(s); WSACleanup(); }
以上是关于socket编程的主要内容,如果未能解决你的问题,请参考以下文章