问题描述
解决方案
原因是多了'n':
scanf("%sn", cmd);
改成
scanf("%s", cmd);
也是OK的
解决方案二:
服务端代码:
#include "stdafx.h"
#include
#include
#pragma comment(lib,"ws2_32.lib")
int _tmain(int argc, _TCHAR* argv[])
{
BOOL bret = false;
do
{
WSADATA wsadata;
WSAStartup(MAKEWORD(2, 2), &wsadata);
if (2 != LOBYTE(wsadata.wVersion) || 2 != HIBYTE(wsadata.wVersion))
break;
SOCKET socksever;
socksever = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (INVALID_SOCKET == socksever)
break;
sockaddr_in addrsever = { 0 };
addrsever.sin_family = AF_INET;
addrsever.sin_port = htons(8383);
if (SOCKET_ERROR == bind(socksever, (SOCKADDR*)&addrsever, sizeof(addrsever)))
break;
if (SOCKET_ERROR == listen(socksever, 5))
break;
SOCKET sockclient;
sockclient= accept(socksever, nullptr, nullptr);
if (INVALID_SOCKET == socksever)
break;
char cmd[MAXBYTE] = { 0 };
while (true)
{
printf("please input your cmd:n");
scanf("%sn", cmd);
send(sockclient, cmd, strlen(cmd)+1, 0);
}
system("pause");
bret = true;
} while (false);
WSACleanup();
return 0;
}
解决方案三:
这是客户端的:
#include "stdafx.h"
#include
#include
#include
#pragma comment (lib,"ws2_32.lib")
int _tmain(int argc, _TCHAR* argv[])
{
WSADATA wsadata;
WSAStartup(MAKEWORD(2, 2), &wsadata);
if (2 != LOBYTE(wsadata.wVersion) || 2 != HIBYTE(wsadata.wVersion))
exit(0);
SOCKET sockclient = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (INVALID_SOCKET == sockclient)
exit(0);
sockaddr_in addsever = { 0 };
addsever.sin_family = AF_INET;
addsever.sin_port = htons(8383);
addsever.sin_addr.S_un.S_addr = inet_addr("192.168.23.7");
if (SOCKET_ERROR == connect(sockclient, (SOCKADDR*)&addsever, sizeof(addsever)))
exit(0);
printf("call succeedn");
int recvlen = { 0 };
char message[MAXBYTE] = { 0 };
while (true)
{
recvlen=recv(sockclient, message,MAXBYTE,0);
if (recvlen>0)
{
printf(message);
printf("n");
}
}
WSACleanup();
return 0;
}
解决方案四:
谢谢各位大大,帮小生一看
解决方案五:
谢谢各位大大,帮小生一看
解决方案六:
scanf("%sn", cmd);
把这个换成以下的语句试试(头文件增加 #include ):
std::cin>>cmd;
解决方案七:
客户端接受的消息本来就是通过服务端转发的信息,,