服务器端:

 #include <WINSOCK2.H>
#include <stdio.h>
#pragma comment(lib,"ws2_32.lib")
int main(int argc,char **argv)
{
//创建套接字
WORD myVersionRequest;
WSADATA wsaData;
myVersionRequest=MAKEWORD(,);
int err;
err=WSAStartup(myVersionRequest,&wsaData);
if (!err)
{
printf("已打开套接字,等待连接!\n");
}
else
{
printf("ERROR:嵌套字未打开!");
return ;
}
//进一步绑定套接字
SOCKET serSocket=socket(AF_INET,SOCK_STREAM,);//创建了可识别套接字 //需要绑定的参数
SOCKADDR_IN addr;
addr.sin_family=AF_INET;
addr.sin_addr.S_un.S_addr=htonl(INADDR_ANY);//ip地址
addr.sin_port=htons();//绑定端口 bind(serSocket,(SOCKADDR*)&addr,sizeof(SOCKADDR));//绑定完成
listen(serSocket,);//其中第二个参数代表能够接收的最多的连接数 //////////////////////////////////////////////////////////////////////////
//开始进行监听
//////////////////////////////////////////////////////////////////////////
SOCKADDR_IN clientsocket;
int len=sizeof(SOCKADDR);
while ()
{
SOCKET serConn=accept(serSocket,(SOCKADDR*)&clientsocket,&len);//如果这里不是accept而是conection的话。。就会不断的监听
char sendBuf[]; sprintf(sendBuf,"hello, %s !",inet_ntoa(clientsocket.sin_addr));//找对对应的IP并且将这行字打印到那里
printf("Send:%s\n",sendBuf);
send(serConn,sendBuf,strlen(sendBuf)+,); char receiveBuf[];//接收
recv(serConn,receiveBuf,sizeof(receiveBuf),);
printf("recv:%s\n",receiveBuf); closesocket(serConn);//关闭
WSACleanup();//释放资源的操作
return ;
}
return ;
}

客户机端:

 #include <WINSOCK2.H>
#include <stdio.h>
#pragma comment(lib,"ws2_32.lib")
int main(int argc,char **argv)
{
int err;
WORD versionRequired;
WSADATA wsaData;
versionRequired=MAKEWORD(,);
err=WSAStartup(versionRequired,&wsaData);//协议库的版本信息
if (!err) {
printf("客户端嵌套字已经打开!\n");
} else {
printf("ERROR:客户端的嵌套字打开失败!\n");
return ;//结束
}
SOCKET clientSocket=socket(AF_INET,SOCK_STREAM,); SOCKADDR_IN clientsock_in;
clientsock_in.sin_addr.S_un.S_addr=inet_addr("192.168.1.3");
clientsock_in.sin_family=AF_INET;
clientsock_in.sin_port=htons(); //bind(clientSocket,(SOCKADDR*)&clientsock_in,strlen(SOCKADDR));//注意第三个参数
//listen(clientSocket,5);
connect(clientSocket,(SOCKADDR*)&clientsock_in,sizeof(SOCKADDR));//开始连接
char receiveBuf[];
if() { printf("Send:%s\n","hello,this is client");
send(clientSocket,"hello,this is client",strlen("hello,this is client")+,); recv(clientSocket,receiveBuf,,);
printf("Recv:%s\n",receiveBuf); closesocket(clientSocket);
WSACleanup();
}
return ;
}

simple "hello world" program:

Client:

 #include<stdio.h>
#include<winsock2.h>
#pragma comment (lib,"ws2_32")
int main(int argc,char *argv[])
{ WSADATA wsaData;
WSAStartup(MAKEWORD(,),&wsaData); SOCKET s=socket(PF_INET,SOCK_STREAM,IPPROTO_TCP); sockaddr_in sockaddr;
sockaddr.sin_family=PF_INET;
sockaddr.sin_addr.S_un.S_addr=inet_addr("127.0.0.1");
sockaddr.sin_port=htons(); connect(s,(SOCKADDR*)&sockaddr,sizeof(SOCKADDR)); char Buffer[MAXBYTE]={};
recv(s,Buffer,MAXBYTE,NULL);
printf("Buffer = %s \r\n",Buffer);
closesocket(s);
getchar();
WSACleanup();
return ; }

Server:

 #include<winsock2.h>
#pragma comment (lib,"ws2_32")
int main(int argc,char *argv[])
{
WSADATA wsaData;
WSAStartup(MAKEWORD(,),&wsaData); SOCKET s=socket(PF_INET,SOCK_STREAM,IPPROTO_TCP); sockaddr_in sockaddr;
sockaddr.sin_family=PF_INET;
sockaddr.sin_addr.S_un.S_addr=inet_addr("127.0.0.1");
sockaddr.sin_port=htons(); bind(s,(SOCKADDR*)&sockaddr,sizeof(SOCKADDR));
listen(s,);
SOCKADDR clientAddr;
int nSize=sizeof(SOCKADDR);
SOCKET clientSock;
clientSock=accept(s,(SOCKADDR*)&clientAddr,&nSize); send(clientSock,"hello client \r\n",strlen("hello client \r\n")+sizeof(char),NULL);
closesocket(clientSock);
closesocket(s); WSACleanup();
return ;
}
04-14 02:18