C++ socket 客户端发送一条用户输入的字符串到服务端,服务端显示不出来
客户端:#include<stdio.h>#include<Winsock2.h>#include<iostream>#include<string>#include<s...
客户端:
#include <stdio.h>
#include <Winsock2.h>
#include <iostream>
#include<string>
#include <sstream>
using namespace std;
#pragma comment(lib, "ws2_32.lib")
void main()
{
WORD wVersionRequested;
WSADATA wsaData;
int err;
string older;
string ip;
string port;
string input;
std::stringstream ss;
string stask;
int iport;
string task;
char c[20];
wVersionRequested = MAKEWORD( 1, 1 );
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 ) {
return;
}
if ( LOBYTE( wsaData.wVersion ) != 1 ||
HIBYTE( wsaData.wVersion ) != 1 ) {
WSACleanup( );
return;
}
SOCKET sockClient=socket(AF_INET,SOCK_STREAM,0);
SOCKADDR_IN addrSrv;
addrSrv.sin_family=AF_INET;
cout<<"欢迎使用牛逼版socket客户端"<<endl;
getline(cin, older, ' ');
getline(cin, ip, ' ');
getline(cin, port, ' ');
if(older=="conn")
{
cout<<"lianjie"<<endl;
cout<<ip<<endl;
cout<<port<<endl;
}
ss<<port;
ss>>iport;
addrSrv.sin_addr.S_un.S_addr=inet_addr(ip.c_str());
addrSrv.sin_port=htons(iport);
connect(sockClient,(SOCKADDR*)&addrSrv,sizeof(SOCKADDR));
char recvBuf[50];
recv(sockClient,recvBuf,50,0);
printf("%s\n",recvBuf);
send(sockClient,"hello",strlen("hello")+1,0);
send(sockClient,"world",strlen("world")+1,1);
cin>>task;
stask=task;
strcpy(c,stask.c_str());
while(strcmp(c,"disconn")!=0)
{
cout<<stask.c_str()<<endl;
char buf[50];
strcmp(stask.c_str(),buf);
send(sockClient,buf,strlen(buf)+1,2);
cin>>task;
stask=task;
strcpy(c,stask.c_str());
}
system("pause");
closesocket(sockClient);
WSACleanup();
}
我知道怎么解决了,不能用cin得到输入数据,要用cin.getline()得到,但是具体为什么我不知道啊 展开
#include <stdio.h>
#include <Winsock2.h>
#include <iostream>
#include<string>
#include <sstream>
using namespace std;
#pragma comment(lib, "ws2_32.lib")
void main()
{
WORD wVersionRequested;
WSADATA wsaData;
int err;
string older;
string ip;
string port;
string input;
std::stringstream ss;
string stask;
int iport;
string task;
char c[20];
wVersionRequested = MAKEWORD( 1, 1 );
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 ) {
return;
}
if ( LOBYTE( wsaData.wVersion ) != 1 ||
HIBYTE( wsaData.wVersion ) != 1 ) {
WSACleanup( );
return;
}
SOCKET sockClient=socket(AF_INET,SOCK_STREAM,0);
SOCKADDR_IN addrSrv;
addrSrv.sin_family=AF_INET;
cout<<"欢迎使用牛逼版socket客户端"<<endl;
getline(cin, older, ' ');
getline(cin, ip, ' ');
getline(cin, port, ' ');
if(older=="conn")
{
cout<<"lianjie"<<endl;
cout<<ip<<endl;
cout<<port<<endl;
}
ss<<port;
ss>>iport;
addrSrv.sin_addr.S_un.S_addr=inet_addr(ip.c_str());
addrSrv.sin_port=htons(iport);
connect(sockClient,(SOCKADDR*)&addrSrv,sizeof(SOCKADDR));
char recvBuf[50];
recv(sockClient,recvBuf,50,0);
printf("%s\n",recvBuf);
send(sockClient,"hello",strlen("hello")+1,0);
send(sockClient,"world",strlen("world")+1,1);
cin>>task;
stask=task;
strcpy(c,stask.c_str());
while(strcmp(c,"disconn")!=0)
{
cout<<stask.c_str()<<endl;
char buf[50];
strcmp(stask.c_str(),buf);
send(sockClient,buf,strlen(buf)+1,2);
cin>>task;
stask=task;
strcpy(c,stask.c_str());
}
system("pause");
closesocket(sockClient);
WSACleanup();
}
我知道怎么解决了,不能用cin得到输入数据,要用cin.getline()得到,但是具体为什么我不知道啊 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询