UDP通信

//************************服务器端***************

#include<stdio.h>

#include<winsock2.h>
#include <memory.h>


void main()
{
WORD wVersionRequested;
WSADATA wsaData;
int err;

wVersionRequested = MAKEWORD( 2, 2 );

err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 ) {
/* Tell the user that we could not find a usable */
/* WinSock DLL.                                  */
return;
}

/* Confirm that the WinSock DLL supports 2.2.*/
/* Note that if the DLL supports versions greater    */
/* than 2.2 in addition to 2.2, it will still return */
/* 2.2 in wVersion since that is the version we      */
/* requested.                                        */

if ( LOBYTE( wsaData.wVersion ) != 2 ||
        HIBYTE( wsaData.wVersion ) != 2 ) {
/* Tell the user that we could not find a usable */
/* WinSock DLL.                                  */
WSACleanup( );
return; 
}

/* The WinSock DLL is acceptable. Proceed. */

SOCKET socket_server=socket(AF_INET,SOCK_DGRAM,0);

SOCKADDR_IN  sin;
sin.sin_port=htons(6000);
sin.sin_family=AF_INET;
sin.sin_addr.S_un.S_addr=htonl(INADDR_ANY);

bind(socket_server,(SOCKADDR*)&sin,sizeof(SOCKADDR));


char recv_buf[200];
char send_buf[200];
char temp[250];
memset(recv_buf,'\0',200);
memset(send_buf,'\0',200);
memset(temp,'\0',250);


SOCKADDR_IN  addr_sin;
int len=sizeof(SOCKADDR);


while(1)
{
recvfrom(socket_server,recv_buf,200,0,(SOCKADDR *)&addr_sin,&len);
sprintf(temp,"%s说 :%s",inet_ntoa(addr_sin.sin_addr),recv_buf);
printf("%s\n",temp);

//sendto(socket_server,"Welcome TO Here !",strlen("Welcome TO Here !")+1,0,(SOCKADDR *)&addr_sin,len);
printf("我说 :");
gets(send_buf);
sendto(socket_server,send_buf,strlen(send_buf)+1,0,(SOCKADDR *)&addr_sin,len);


}
        closesocket(socket_server);
        WSACleanup();




}


//************************客户端**************************************

#include<stdio.h>
#include<winsock2.h>
void main()
{
WORD wVersionRequested;
WSADATA wsaData;
int err;

wVersionRequested = MAKEWORD( 2, 2 );

err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 ) {
/* Tell the user that we could not find a usable */
/* WinSock DLL.                                  */
return;
}

/* Confirm that the WinSock DLL supports 2.2.*/
/* Note that if the DLL supports versions greater    */
/* than 2.2 in addition to 2.2, it will still return */
/* 2.2 in wVersion since that is the version we      */
/* requested.                                        */

if ( LOBYTE( wsaData.wVersion ) != 2 ||
        HIBYTE( wsaData.wVersion ) != 2 ) {
/* Tell the user that we could not find a usable */
/* WinSock DLL.                                  */
WSACleanup( );
return; 
}

/* The WinSock DLL is acceptable. Proceed. */

SOCKET socket_client=socket(AF_INET,SOCK_DGRAM,0);

SOCKADDR_IN  sin;
sin.sin_port=htons(6000);
sin.sin_family=AF_INET;
sin.sin_addr.S_un.S_addr=inet_addr("127.0.0.1");



char recv_buf[200];
char send_buf[200];
char temp[250];
memset(recv_buf,0,200);
memset(send_buf,0,200);
memset(temp,0,250);


int len=sizeof(SOCKADDR);
while(1)
{
        //printf("Please Input your data !\n");
//sendto(socket_clien,"I am client !",strlen("I am client !")+1,0,(SOCKADDR *)&sin,len);
printf("我说 :");
gets(send_buf);
sendto(socket_clien,send_buf,strlen(send_buf)+1,0,(SOCKADDR *)&sin,len);

recvfrom(socket_clien,recv_buf,200,0,(SOCKADDR *)&sin,&len);
sprintf(temp,"%s说 :%s",inet_ntoa(sin.sin_addr),recv_buf);
printf("%s\n",temp);




}
        closesocket(socket_clien);
        WSACleanup();


}


你可能感兴趣的:(UDP通信)