c语言实现基于C/S的echo 1C
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 input\n");
while(scanf("%[^EOF]%*c",&buf1)==EOF)
{
    break;
}
scanf("%[^EOF]",&buf1);
scanf("%[^\n]",&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: %s\n",inet_ntoa(server.sin_addr));
printf("server Ip port: %d\n",ntohs(server.sin_port));
printf("%s\n",buf2);
closesocket(sclient);
WSACleanup();
c

1个回答

问题是什么,是因为scanf不返回吗?
%[^EOF]在读取一系列字符,只要输入不包含E或O或F,就不会停止

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问