请问用多线程的方法改写socket的c代码可以给我发一份嘛?感激不尽!
1个回答
2017-07-13
展开全部
#include "stdio.h"
#include "winsock2.h"
#include "stdlib.h"
#pragma comment(lib,"ws2_32")
void main()
{
WSADATA wsaData;
WSAStartup(MAKEWORD(2,2),&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(9000);
connect(s,(SOCKADDR*)&sockaddr,sizeof(SOCKADDR));
char buffer[MAXBYTE] = {0};
recv(s,buffer,MAXBYTE,NULL);
printf(" ***SERVER*** %s",buffer);
while(TRUE)
{
char* mymsg = new char[10000];
printf(" OK Now You Can Connect .\n ->");
gets(mymsg);
send(s,mymsg,strlen(mymsg)+sizeof(char),NULL);
}
closesocket(s);
WSACleanup();
getchar();
exit(0);
}
#include "winsock2.h"
#include "stdlib.h"
#pragma comment(lib,"ws2_32")
void main()
{
WSADATA wsaData;
WSAStartup(MAKEWORD(2,2),&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(9000);
connect(s,(SOCKADDR*)&sockaddr,sizeof(SOCKADDR));
char buffer[MAXBYTE] = {0};
recv(s,buffer,MAXBYTE,NULL);
printf(" ***SERVER*** %s",buffer);
while(TRUE)
{
char* mymsg = new char[10000];
printf(" OK Now You Can Connect .\n ->");
gets(mymsg);
send(s,mymsg,strlen(mymsg)+sizeof(char),NULL);
}
closesocket(s);
WSACleanup();
getchar();
exit(0);
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询