21,458
社区成员
发帖
与我相关
我的任务
分享
#include <winsock2.h>
#include <stdio.h>
#pragma comment (lib,"ws2_32.lib")
int main(int argc,char *argv[])
{
WORD wVersionRequested;
WSADATA wsaData;
int err;
char recvBuf[100];
SOCKET socClient;
SOCKADDR_IN addrSvr;
wVersionRequested = MAKEWORD(2,2);
if((err = WSAStartup(wVersionRequested,&wsaData)) != 0)
{
printf("WSAStartup error!\n");
exit(1);
}
if(LOBYTE(wsaData.wVersion) != 2||HIBYTE(wsaData.wVersion) != 2)
{
WSACleanup();
printf("Pls check socket version!\n");
exit(1);
}
while (1)
{
if(socClient = socket(AF_INET,SOCK_STREAM,0) == INVALID_SOCKET)
{
printf("Create socket fail!\n");
exit(0);
}
addrSvr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
addrSvr.sin_family = AF_INET;
addrSvr.sin_port = htons(8080);
if(connect(socClient,(SOCKADDR *)&addrSvr,sizeof(SOCKADDR)) == SOCKET_ERROR)
{
printf("Connect socket fail!\n");
exit(0);
}
if(send(socClient,"this is cxl request",strlen("this is cxl request")+1,0) == SOCKET_ERROR)
{
printf("Send data fail!\n");
exit(0);
}
if(recv(socClient,recvBuf,100,0) == SOCKET_ERROR)
{
printf("Recve data fail!\n");
exit(0);
}
printf("Get data from server%s\n",recvBuf);
closesocket(socClient);
}
WSACleanup();
return 0;
}
#include <winsock2.h>
#include <stdlib.h>
#pragma comment (lib,"ws2_32.lib")
int main(int argv,char *argc[])
{
WORD wVersionRequested;
WSADATA wsaData;
int err;
int len;
char recBuf[100];
char sendBuf[100];
SOCKET socSvr;
SOCKET socCon;
SOCKADDR_IN addrSvr;
SOCKADDR_IN addrCli;
wVersionRequested = MAKEWORD(2,2);
err = WSAStartup(wVersionRequested,&wsaData);
if(err != 0)
{
printf("Pls check the version of socket!\n");
exit(1);
}
if(LOBYTE(wsaData.wVersion) != 2||HIBYTE(wsaData.wVersion) != 2)
{
printf("wsaData.wVersion != 2\n");
WSACleanup();
exit(1);
return 0;
}
if((socSvr = socket(AF_INET,SOCK_STREAM,0)) == INVALID_SOCKET)
{
printf("Create socket fail!\n");
exit(0);
}
addrSvr.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
addrSvr.sin_family = AF_INET;
addrSvr.sin_port = htons(8080);
if(bind(socSvr,(SOCKADDR *)&addrSvr,sizeof(SOCKADDR)) == SOCKET_ERROR)
{
printf("bind socket fail!\n");
exit(0);
}
if(listen(socSvr,5) == SOCKET_ERROR)
{
printf("listen socket fail!\n");
exit(0);
}
len = sizeof(SOCKADDR);
while(1)
{
//接受客户端的请求,并与客户端建立连接
if((socCon = accept(socSvr,(SOCKADDR *)&addrCli,&len)) == INVALID_SOCKET)
{
printf("create connect with client fail!\n");
exit(0);
}
if(recv(socCon,recBuf,100,0) == SOCKET_ERROR )
{
printf("recv data fail!\n");
exit(0);
}
else
{
printf("get client data:%\s\n",recBuf);
}
sprintf(sendBuf,"Welcome %s to cxl server",inet_ntoa(addrCli.sin_addr));
if(send(socCon,sendBuf,strlen(sendBuf)+1,0) == SOCKET_ERROR)
{
printf("send data fail!\n");
exit(0);
}
closesocket(socCon);
}
WSACleanup();
return 0;
}
#include <winsock2.h>
#include <stdio.h>
#pragma comment (lib,"ws2_32.lib")
int main(int argc,char *argv[])
{
WORD wVersionRequested;
WSADATA wsaData;
int err;
char recvBuf[100];
SOCKET socClient;
SOCKADDR_IN addrSvr;
wVersionRequested = MAKEWORD(2,2);
if((err = WSAStartup(wVersionRequested,&wsaData)) != 0)
{
printf("WSAStartup error!\n");
exit(1);
}
if(LOBYTE(wsaData.wVersion) != 2||HIBYTE(wsaData.wVersion) != 2)
{
WSACleanup();
printf("Pls check socket version!\n");
exit(1);
}
while (1)
{ //这个地方少括号了!符号优先级问题
if((socClient = socket(AF_INET,SOCK_STREAM,0)) == INVALID_SOCKET)
{
printf("Create socket fail!\n");
exit(0);
}
addrSvr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
addrSvr.sin_family = AF_INET;
addrSvr.sin_port = htons(8080);
if(connect(socClient,(SOCKADDR *)&addrSvr,sizeof(SOCKADDR)) == SOCKET_ERROR)
{
printf("Connect socket fail!\n");
exit(0);
}
if(send(socClient,"this is cxl request",strlen("this is cxl request")+1,0) == SOCKET_ERROR)
{
printf("Send data fail!\n");
exit(0);
}
if(recv(socClient,recvBuf,100,0) == SOCKET_ERROR)
{
printf("Recve data fail!\n");
exit(0);
}
printf("Get data from server%s\n",recvBuf);
closesocket(socClient);
}
WSACleanup();
return 0;
}