zoukankan      html  css  js  c++  java
  • 基于udp协议的字符串计数【网络程序设计

    udp_server.c

    #include <stdio.h>
    #include <winsock2.h>
     
    #pragma comment(lib, "ws2_32.lib") 
    typedef struct sockaddr_in sockaddr_in ;
    typedef struct sockaddr sockaddr;
    
    int main(int argc, char* argv[])
    {
    	WSADATA wsaData;
    	WORD sockVersion = MAKEWORD(2,2);
    	if(WSAStartup(sockVersion, &wsaData) != 0)
    	{
    		return 0;
    	}
     
    	SOCKET serSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); 
    	if(serSocket == INVALID_SOCKET)
    	{
    		printf("socket error !");
    		return 0;
    	}
     
    	sockaddr_in serAddr;
    	serAddr.sin_family = AF_INET;
    	serAddr.sin_port = htons(8888);
    	serAddr.sin_addr.S_un.S_addr = INADDR_ANY;
    	if(bind(serSocket, (sockaddr *)&serAddr, sizeof(serAddr)) == SOCKET_ERROR)
    	{
    		printf("bind error !");
    		closesocket(serSocket);
    		return 0;
    	}
    	
    	sockaddr_in remoteAddr;
    	int nAddrLen = sizeof(remoteAddr); 
    	while (1)
    	{
    		char recvData[255];  
    		int ret = recvfrom(serSocket, recvData, 255, 0, (sockaddr *)&remoteAddr, &nAddrLen);
    		//puts(recvData); 
    		if (ret > 0)
    		{
    			recvData[ret] = 0x00;			
    			printf("接受到一个连接:%s 
    ", inet_ntoa(remoteAddr.sin_addr));
    			printf(recvData);
    			puts("");			
    		}
    		int temp=strlen(recvData);
    		char sendData[255];
    		sprintf(sendData,"%d",temp);
    		//char * sendData = "一个来自服务端的UDP数据包
    ";
    		sendto(serSocket, sendData, strlen(sendData), 0, (sockaddr *)&remoteAddr, nAddrLen);	
     
    	}
    	closesocket(serSocket); 
    	WSACleanup();
    	return 0;
    }
    

    udp_client.c

    #include <stdio.h>
    #include <winsock2.h>
    
    #pragma comment(lib, "ws2_32.lib")
    typedef struct sockaddr_in sockaddr_in ;
    typedef struct sockaddr sockaddr ;
    
    int main(int argc, char* argv[])
    {
    	WORD socketVersion = MAKEWORD(2,2);
    	WSADATA wsaData;
    	if(WSAStartup(socketVersion, &wsaData) != 0)
    	{
    		return 0;
    	}
    	SOCKET sclient = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
    
    	sockaddr_in sin;
    	sin.sin_family = AF_INET;
    	sin.sin_port = htons(8888);
    	sin.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
    	int len = sizeof(sin);
    
    	char sendData[256];
    	while(1)
    	{		
    		 gets(sendData);
    		 //"来自客户端的数据包.
    ";
    		 if(strcmp(sendData,"quit")==0)
    			 break;
    		sendto(sclient, sendData, strlen(sendData), 0, (sockaddr *)&sin, len);
    
    		char recvData[255];
    		int ret = recvfrom(sclient, recvData, 255, 0, (sockaddr *)&sin, &len);
    		if(ret > 0)
    		{
    			recvData[ret] = 0x00;
    			printf(recvData);
    			puts("");
    		}
    
    	}
    
    
    	closesocket(sclient);
    	WSACleanup();
    	return 0;
    }
    
    
  • 相关阅读:
    POJ 1659 Frogs' Neighborhood
    zoj 2913 Bus Pass(BFS)
    ZOJ 1008 Gnome Tetravex(DFS)
    POJ 1562 Oil Deposits (DFS)
    zoj 2165 Red and Black (DFs)poj 1979
    hdu 3954 Level up
    sgu 249 Matrix
    hdu 4417 Super Mario
    SPOJ (BNUOJ) LCM Sum
    hdu 2665 Kth number 划分树
  • 原文地址:https://www.cnblogs.com/shengwang/p/9944204.html
Copyright © 2011-2022 走看看