求VC++使用“TCP/IP”和“UDP”这些协议的Demo
求VC++使用“TCP/IP”和“UDP”这些协议的Demo...
[解决办法]
http://www.vckbase.com/document/finddoc.asp?keyword=TCP
[解决办法]
- C/C++ code
//************************服务器端***************#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();}
------解决方案--------------------
看看Windows网络编程的书,一般都有例子代码的,MSDN上应该也是有的
[解决办法]
http://download.csdn.net/detail/jjunjoe/2430752
正好能满足你的所有要求。