源代码项目下载:http://download.csdn.net/detail/mmyzlinyingjie/7049499
TCP类的:
TcpServer.cpp
//WSAStartup socket bind listen accept send/recv closesocket WSACleanup
#include <winsock2.h>
#include <stdio.h>
#include <iostream>
#include <windows.h>
using namespace std;
#pragma comment (lib,"ws2_32")
#pragma comment (lib,"winmm") #define NAME_LEN 20 int main(int argc , char* grgv[])
{ cout<<"This is Server cmd"<<endl;
WSADATA wsaData;
WSAStartup(MAKEWORD(,),&wsaData); //这个是服务器的socket,用来绑定网卡来监听
SOCKET s = socket(PF_INET,SOCK_STREAM,IPPROTO_TCP); sockaddr_in addr;
addr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
addr.sin_port = htons();
addr.sin_family = PF_INET; bind(s,(SOCKADDR*)&addr,sizeof(SOCKADDR)); listen(s,); SOCKADDR clientAddr;
int nSize = sizeof(SOCKADDR); //这个socket用来接收客户端发过来的信息
SOCKET clientSock; //阻塞式,有客户端来连接时才会往下执行
//执行到这一步,说明客户端和服务器已经建立了连接
clientSock = accept(s,(SOCKADDR*)&clientAddr,&nSize); cout<<"sending..."<<endl;
send(clientSock,"hello client \r\n",strlen("hello client \r\n" + sizeof(char)),NULL);
cout<<"sended"<<endl; //不断的接收客户端发送过来的信息
while(TRUE)
{ char buff[MAXBYTE] = {};
//接收客户端发来的命令
int i=;
i = recv(clientSock,buff,MAXBYTE,);
if(i != -)
cout<<"from client :"<<buff<<endl; } closesocket(clientSock);
closesocket(s); WSACleanup();
return ;
}
TcpClient.cpp
#include <stdio.h>
#include <winsock2.h>
#include <iostream>
#include <conio.h>
#include <string>
using namespace std;
#pragma comment (lib,"ws2_32") #define NAME_LEN 20 int main(int argc,char* argv[])
{
cout<<"This is Client cmd"<<endl;
WSADATA wsaData;
WSAStartup(MAKEWORD(,),&wsaData); //创建一个socket来连接服务端
SOCKET s = socket(PF_INET,SOCK_STREAM,IPPROTO_TCP); sockaddr_in addr;
addr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
addr.sin_family = PF_INET;
addr.sin_port = htons(); //这条执行后,说明和服务端建立了连接
connect(s,(SOCKADDR*)&addr,sizeof(SOCKADDR)); char Buff[MAXBYTE] = {};
char Cmd[MAXBYTE] = {};
recv(s,Buff , MAXBYTE,);
cout<<"from server:"<<Buff<<endl; //不断地给服务端发送信息
while(TRUE)
{
cin>>Cmd;
cout<<"you input command is:"<<Cmd<<endl;
send(s,Cmd,MAXBYTE,); }
getchar();
WSACleanup();
return ;
}
运行结果:
UDP:
UDPServer.cpp
#include <winsock2.h>
#include <stdio.h>
#include <iostream>
#include <windows.h>
using namespace std;
#pragma comment (lib,"ws2_32")
#pragma comment (lib,"winmm") int main(){ printf("This is UDP Server\n"); WORD wVersionRequested;
WSADATA wsaData; wVersionRequested = MAKEWORD( , );
WSAStartup( wVersionRequested, &wsaData ); SOCKET socketServer = socket(AF_INET,SOCK_DGRAM,); SOCKADDR_IN addr;
addr.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
addr.sin_family = AF_INET;
addr.sin_port = htons(); bind(socketServer,(SOCKADDR*)&addr,sizeof(SOCKADDR)); int len = sizeof(SOCKADDR);
SOCKADDR_IN addrClient;
char buf[];
recvfrom(socketServer,buf,sizeof(buf),,(SOCKADDR*)&addrClient,&len);
printf("%s",buf); closesocket(socketServer);
WSACleanup();
return ;
}
UDPClient.cpp
#include <winsock2.h>
#include <stdio.h>
#include <iostream>
#include <windows.h>
using namespace std;
#pragma comment (lib,"ws2_32")
#pragma comment (lib,"winmm")
int main(){ printf("This is UDP Client\n");
WORD wVersionRequested;
WSADATA wsaData; wVersionRequested = MAKEWORD( , );
WSAStartup( wVersionRequested, &wsaData ); SOCKET socketClient = socket(AF_INET,SOCK_DGRAM,); SOCKADDR_IN addr;
addr.sin_addr.S_un.S_addr = inet_addr("10.21.23.117");//"10.21.23.117"是你要发送的目的地
addr.sin_family = AF_INET;
addr.sin_port = htons(); char buf[];
sprintf(buf,"i MYNONA from cclient\n",inet_ntoa(addr.sin_addr));
sendto(socketClient,buf,sizeof(buf)+,,(SOCKADDR*)&addr,sizeof(SOCKADDR)); closesocket(socketClient);
WSACleanup(); return ;
}
运行结果: