server端代码
#include <stdio.h>
#include <winsock2.h>
#pragma comment(lib,"ws2_32.lib")
int ret,error;
char buf[256],str[256];
SOCKET s,s_new;
void main(){
HWND hWnd;
WSADATA wsaData;
WSAStartup(MAKEWORD(2,2),&wsaData);
//创建socket
s=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
//为监听的socket指定一个地址及端口
SOCKADDR_IN addr;
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = inet_addr("127.0.0.1");
addr.sin_port = htons(8888);//htons 本机顺序 转化为 网络字节顺序
//绑定端口
ret = bind(s,(LPSOCKADDR)&addr,sizeof(addr));
if(ret==SOCKET_ERROR)
{
error = WSAGetLastError();
MessageBox(hWnd,"bind error","prompt",MB_OK);
closesocket(s);
s=NULL;
WSACleanup();
return;
}
printf("%s/n","bindsuccess");//有可能不成功因为端口可能会有冲突
//可在setsockopt 函数中设置套接字选项SO_REUSEADDR
//允许多个套接字访问同一个本地I P 地址及端口号
//监听
ret = listen(s,SOMAXCONN);
if(ret==SOCKET_ERROR)
{
printf("%s/n","listenerror");
error = WSAGetLastError();
MessageBox(hWnd,"listen error","prompt",MB_OK);
closesocket(s);
s=NULL;
WSACleanup();
return;
}
printf("%s/n","listensuccess");
//接收客户端连接请求
s_new = accept(s,NULL,NULL);//accept函数用于接收一条新的连接而不是接收数据
//若成功则返回一个新的socket套接字
printf("%d/n",s_new);
if(s_new==SOCKET_ERROR)
{
printf("%s/n","accepterror");
error = WSAGetLastError();
MessageBox(hWnd,"accept error","prompt",MB_OK);
closesocket(s);
s=NULL;
WSACleanup();
return;
}
printf("%s/n","acceptsuccess");
while(true)
{
//接收数据
memset(buf,0,sizeof(buf));
ret = recv(s_new,buf,256,0);
if(ret==SOCKET_ERROR||ret==0)
{
printf("%s/n","receiveerror");
error = WSAGetLastError();
MessageBox(hWnd,"receive error","prompt",MB_OK);
closesocket(s);
s=NULL;
WSACleanup();
return;
}
printf("%s/n",buf);
memset(str,0,sizeof(str));//初始化str
printf("%s/n","please send response");
scanf("%s",str);
//发送数据
if((ret=send(s_new,str,256,0))==SOCKET_ERROR)
{
printf("%s/n","senderror");
error = WSAGetLastError();
MessageBox(hWnd,"send error","prompt",MB_OK);
}
}
}
client端代码
#include <stdio.h>
#include <winsock2.h>
#pragma comment(lib,"ws2_32.lib")
int ret,error;
WSADATA wsaData;
HWND hWnd;
SOCKET s;
char buf[256],str[256];
void main()
{
if((ret = WSAStartup(MAKEWORD(2,2),&wsaData))!=0)
{
printf("%s/n","WSAStartup error");
error = WSAGetLastError();
MessageBox(hWnd,"WSAStartup error","prompt",MB_OK);
return;
}
//创建socket
if((s=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP))==INVALID_SOCKET)
{
printf("%s/n","create socket error");
error = WSAGetLastError();
MessageBox(hWnd,"create socket error","prompt",MB_OK);
WSACleanup();
return;
}
//绑定端口
SOCKADDR_IN ServerAddr;
ServerAddr.sin_family = AF_INET;
ServerAddr.sin_port=htons(8888);
ServerAddr.sin_addr.s_addr=inet_addr("127.0.0.1");
//连接
ret = connect(s,(SOCKADDR*)&ServerAddr,sizeof(ServerAddr));
printf("%d/n",ret);
if(ret==SOCKET_ERROR)
{
printf("%s/n","connect server error");
error = WSAGetLastError();
MessageBox(hWnd,"connect server error","prompt",MB_OK);
closesocket(s);
WSACleanup();
return;
}
while(true){
printf("%s/n","please input the string ");
memset(str,0,sizeof(str));
scanf("%s",str);
//发送数据
if((ret=send(s,str,256,0))==SOCKET_ERROR)
{
printf("%s/n","send error");
error = WSAGetLastError();
MessageBox(hWnd,"send error","prompt",MB_OK);
closesocket(s);
WSACleanup();
return;
}
memset(buf,0,sizeof(buf));
ret = recv(s,buf,256,0);
printf("%s/n",buf);
}
}