求高手指点一个C++写的关于socket多线程通信的连接问题,程序代码如下:创建工程,可以运行
#include<iostream>#include<string>#include<winsock2.h>#include<cstdlib>#pragmacomment...
#include<iostream>
#include<string>
#include<winsock2.h>
#include<cstdlib>
#pragma comment(lib,"ws2_32.lib")
using namespace std;
string str="";
void recvf(SOCKET sockConnect)
{ char bufFanKui[1024]={0};
while(true)
{ if(recv(sockConnect,bufFanKui,sizeof(bufFanKui),0))
{ cout<<"failed---服务器……"<<GetLastError()<<endl;
return ;}
if(str[0]!='\0'){
str += bufFanKui;cout<<str;
} }}
int main(){
system("color 2");
cout<<"服务器启动.....\n";
//初始化winsock
WSADATA wsaData;
int err;
if(err=WSAStartup(MAKEWORD(2,1),&wsaData))
return 1;
if(LOBYTE(wsaData.wVersion)!=2 || HIBYTE(wsaData.wVersion)!=1){
WSACleanup();
return 1;}
//创建监听套接字
SOCKET sclient=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
//绑定地址和监听套接字
sockaddr_in my_addr;//主机地址
memset(&my_addr,0,sizeof(my_addr));//
my_addr.sin_family=AF_INET;
my_addr.sin_addr.S_un.S_addr=htonl(INADDR_ANY);
my_addr.sin_port=htons(8080);//指定端口
if(bind(sclient,(sockaddr*)&my_addr,sizeof(my_addr))==SOCKET_ERROR)
cout<<"failed bind():"<<GetLastError()<<endl;
//启动监听
if(listen(sclient,5)!=0) //10代表最大连接数
cout<<"启动监听失败!\n";
//开始接收数据
sockaddr_in c_addr;//客户地址
int addr_len=sizeof(c_addr);
while(true){ //............................
cout<<"waiting.....";
SOCKET sconnect=accept(sclient,(struct sockaddr*)&c_addr,&addr_len);//通过监听套接字创建连接
if(sconnect==INVALID_SOCKET)//INVALID_SOCKET=-1无效的套接字
{cout<<"invalid socket\n";
return 0;}
cout<<"链接成功...."<<endl;
CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)recvf,(void*)sclient,0,NULL);
cout<<"服务器说: ";
while(true)
{char buf1[1024]="";
cin.getline(buf1,1024);
send(sclient,buf1,sizeof(buf1),0);
cout<<"服务器说: ";}
closesocket(sclient);}
WSACleanup();
system("pause");
return 0;
}
//这是服务器上的代码,下面是客户端上的代码:http://zhidao.baidu.com/question/570394030.html
错误如截图: 展开
#include<string>
#include<winsock2.h>
#include<cstdlib>
#pragma comment(lib,"ws2_32.lib")
using namespace std;
string str="";
void recvf(SOCKET sockConnect)
{ char bufFanKui[1024]={0};
while(true)
{ if(recv(sockConnect,bufFanKui,sizeof(bufFanKui),0))
{ cout<<"failed---服务器……"<<GetLastError()<<endl;
return ;}
if(str[0]!='\0'){
str += bufFanKui;cout<<str;
} }}
int main(){
system("color 2");
cout<<"服务器启动.....\n";
//初始化winsock
WSADATA wsaData;
int err;
if(err=WSAStartup(MAKEWORD(2,1),&wsaData))
return 1;
if(LOBYTE(wsaData.wVersion)!=2 || HIBYTE(wsaData.wVersion)!=1){
WSACleanup();
return 1;}
//创建监听套接字
SOCKET sclient=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
//绑定地址和监听套接字
sockaddr_in my_addr;//主机地址
memset(&my_addr,0,sizeof(my_addr));//
my_addr.sin_family=AF_INET;
my_addr.sin_addr.S_un.S_addr=htonl(INADDR_ANY);
my_addr.sin_port=htons(8080);//指定端口
if(bind(sclient,(sockaddr*)&my_addr,sizeof(my_addr))==SOCKET_ERROR)
cout<<"failed bind():"<<GetLastError()<<endl;
//启动监听
if(listen(sclient,5)!=0) //10代表最大连接数
cout<<"启动监听失败!\n";
//开始接收数据
sockaddr_in c_addr;//客户地址
int addr_len=sizeof(c_addr);
while(true){ //............................
cout<<"waiting.....";
SOCKET sconnect=accept(sclient,(struct sockaddr*)&c_addr,&addr_len);//通过监听套接字创建连接
if(sconnect==INVALID_SOCKET)//INVALID_SOCKET=-1无效的套接字
{cout<<"invalid socket\n";
return 0;}
cout<<"链接成功...."<<endl;
CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)recvf,(void*)sclient,0,NULL);
cout<<"服务器说: ";
while(true)
{char buf1[1024]="";
cin.getline(buf1,1024);
send(sclient,buf1,sizeof(buf1),0);
cout<<"服务器说: ";}
closesocket(sclient);}
WSACleanup();
system("pause");
return 0;
}
//这是服务器上的代码,下面是客户端上的代码:http://zhidao.baidu.com/question/570394030.html
错误如截图: 展开
1个回答
更多追问追答
追问
你用的这个软件是什么编译软件?如何修改?
追答
vc6,vs.. 都有查找错误号的,工具---错误查找;
1.你服务端这边参数传错了:
CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)recvf,(void*)sclient,0,NULL);
//改为
CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)recvf,(void*)sconnect,0,NULL);
2.接收函数也有点问题, recv if语句也没写完整
void recvf(SOCKET sockConnect)
{
char bufFanKui[1024]={0};
while(true)
{
if(recv(sockConnect,bufFanKui,sizeof(bufFanKui),0) == -1 )//这里判断-1
{
cout<<"failed---服务器……"<<GetLastError()<<endl;
return ;
}
printf("%s \n",bufFanKui); //加上这行
if(str[0] !='\0') //这个没用...
{
str += bufFanKui;
cout<<str;
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询