程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> socket-C++Socket網絡連接問題

socket-C++Socket網絡連接問題

編輯:編程綜合問答
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;
}

最佳回答:


用sniffer pro看下,是不是根本沒有收到,檢查防火牆的設置。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved