WSADATA wsadata;
这是客户端代码,不知道要怎么结束
SOCKET sclient;
sockaddr_in server;
int iport;
//char buf1[]="Hello,I am client!";
char buf1[1024];
char buf2[1024];//接收数据
memset(buf2,0,sizeof(buf2));
if(argc<3)
{
printf("Usage:client [server IP address] [server port]\n");
return;
}
iport=atoi(argv[2]);
printf("please inputn");
while(scanf("%1%*c",&buf1)==EOF)
{
break;
}
scanf("%1",&buf1);
scanf("%2",&buf1);//从键盘读入字符串,但是此时有考虑空格
if(WSAStartup(MAKEWORD(2,2),&wsadata)!=0)
{
printf("cuowu1\n");
return;
}
sclient=socket(AF_INET,SOCK_STREAM,0);
if(sclient==INVALID_SOCKET)
{
printf("cuowu2\n");
return;
}
server.sin_family=AF_INET;
server.sin_port=htons(iport);
server.sin_addr.s_addr=inet_addr(argv[1]);
if(connect(sclient,(struct sockaddr*)&server,sizeof(server))==INVALID_SOCKET)
{
printf("cuowu3\n");
return;
}
if(send(sclient,buf1,sizeof(buf1),0)==SOCKET_ERROR)
{
printf("cuowu4\n");
}
if(recv(sclient,buf2,sizeof(buf2),0)==SOCKET_ERROR)
{
printf("cuowu5\n");
return;
}
printf("server Ip address: %sn",inet_ntoa(server.sin_addr));
printf("server Ip port: %dn",ntohs(server.sin_port));
printf("%sn",buf2);
closesocket(sclient);
WSACleanup();
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。