流云飞水
2015-07-29 16:47
采纳率: 50%
浏览 2.2k

C++Socket网络连接问题

在用C++编程时,使用socket,在本机上自我链接成功,无论是127.0.0.1还是真实ip都可以连接成功,而在两台电脑 之间就会失败, 求指教,可能会发生什么问题, 以下是源代码
void * JoinScene::thread_funcation(void arg)
{
/
客户端编程*/
log("ip:%s",AimIp);
WORD wVersionRequested;
WSADATA wsaData; //用于接受Windows Socket 的结构信息
int err;
wVersionRequested = MAKEWORD(1,1); //请求1.1版本的windows 库
err = WSAStartup(wVersionRequested,&wsaData);
if (err !=0)
{
log("I'm failed at first return");
return NULL;
}
if (LOBYTE(wsaData.wVersion)!=1 || HIBYTE(wsaData.wVersion)!=1){
WSACleanup(); // 否则用这个函数清除信息,结束
log("I'm failed at second return");
return NULL;
}
socket_fd = socket(AF_INET,SOCK_STREAM,0);
if (-1 == socket_fd){
log("client socket falid");
return NULL;
}
sockaddr_in setverAddress; //服务器的地址
setverAddress.sin_port = htons(8888); //iP
setverAddress.sin_family = AF_INET;
setverAddress.sin_addr.S_un.S_addr = inet_addr(AimIp);
int connect_fd = connect(socket_fd,(sockaddr *)&setverAddress,sizeof(setverAddress));
if (connect_fd == -1){
log("contect falid ");
return NULL;
}
int x = 0;
while (true)
{
if(sendData[0] == 'Y')
{
int sendLenght = send(socket_fd,sendData,100,0);
sendData[0] = 'N';
if (sendLenght == -1){
log("send falid");
}

}
if(firstTime)
{
int receLen = recv(socket_fd,receData,100,0);
firstTime = false;
}else
{
recv(socket_fd,Turn,100,0);
recv(socket_fd,receTag,100,0);
recv(socket_fd,receXP,100,0);
recv(socket_fd,receYP,100,0);
}

log("%s",receData);    // 输出服务器传过来的
log("tag %s",receTag);    // 输出服务器传过来的
log("Xp %s",receXP);    // 输出服务器传过来的
log("Yp %s",receYP);    // 输出服务器传过来的
}   
return NULL;

}

void JoinScene::startGame(float dt)
{
if(receData[1] == 's')
{
Director::getInstance()->replaceScene(ParticipateScene::createScene());
}
}
/*将数据处理为网络可传输格式*/
void JoinScene::DataFactory(int tag,float xPosition,float yPosition)
{
sprintf(SendTag,"%d",tag);
gcvt(xPosition,99,SendXP);
gcvt(yPosition,99,SendYP);
log("%s",SendTag);
log("%s",SendXP);
log("%s",SendYP);
SentMessage();
}
/*将网络数据转化为可用的int型*/
int JoinScene::IntFactory()
{
int x = atoi(receTag);
// log("Stoint %d",x);
return x;
}

服务端
// 需要线程来完成的功能都写在这个函数里
void * CreateScene::thread_funcation(void arg)
{
/
实现通过套接字进行网络联接*/
WORD wVersionRequested;
WSADATA wsaData; //用于接受Windows Socket 的结构信息
int err;
wVersionRequested = MAKEWORD(1,1); //请求1.1版本的windows 库
err = WSAStartup(wVersionRequested,&wsaData);
if (err !=0)
{
log("I don't Know");
return NULL;
}
if (LOBYTE(wsaData.wVersion)!=1 || HIBYTE(wsaData.wVersion)!=1){
WSACleanup(); // 否则用这个函数清除信息,结束
log("why!!");
return NULL;
}
int socket_fd = socket(AF_INET,SOCK_STREAM,0);//类型、流式、协议
if (socket_fd == -1){//如果socket返回 -1就 返回,防止崩。。。。
log("sorry...");
return NULL;
}
sockaddr_in serverAddress;
serverAddress.sin_family = AF_INET; //一般来说AF_INET(地址族)PF_INET(协议族)
serverAddress.sin_addr.S_un.S_addr = INADDR_ANY;
serverAddress.sin_port = htons(6666); //必须 加上 htons
int bind_fd = bind(socket_fd,(sockaddr*)(&serverAddress),sizeof(serverAddress));
if (bind_fd == -1){
log("sorry bind failed");
return NULL;
}
if (listen(socket_fd,2) == -1){
log("Listen Failed");
return NULL;
}
sockaddr_in clientAddress;
int s = sizeof(clientAddress);
client_fd = accept(socket_fd,(sockaddr *)&clientAddress,&s);
if (client_fd == -1){
log("accept Failed");
return NULL;
}

while(true)
{
    if(firstTime)
{
int recv_data = recv(client_fd,buf,100,0);
if (recv_data == -1){
    log("receive failed");
}else if(recv_data == 0)
{
    closesocket(socket_fd);
}else
{
    log("here receive %s",buf);     
}   
firstTime = false;
}else
{
recv(client_fd,TurnReceive,100,0);
recv(client_fd,bufTag,100,0);
recv(client_fd,bufXP,100,0);
recv(client_fd,bufYP,100,0);
log("here receive turnreceive %s %d",TurnReceive,bufTag);
}

if(sentData[0]=='Y')
    {
        int sendLenght = send(client_fd,sentData,100,0);
        sentData[0] = 'N';
        if (sendLenght == -1){
        log("send falid");
    }
    }       
}

return NULL;
}

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • threenewbee 2015-07-29 22:02
    已采纳

    用sniffer pro看下,是不是根本没有收到,检查防火墙的设置。

    打赏 评论
  • oyljerry 2015-07-29 16:51

    先查看错误信息,是不是机器上防火墙等阻止连接。

    打赏 评论
  • Ainuokema 2015-08-06 23:10

    防火墙,或者路由器要做映射

    打赏 评论
  • 阿振 2021-03-25 23:19

    127.0.0.1 是本地回环地址,只有本机有效

    打赏 评论

相关推荐 更多相似问题