//************************服务器端***************
#include<stdio.h>
#include<winsock2.h>}
//************************客户端**************************************
#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();
}