2016-09-30 96 views
0

我正在進行TCP/IP連接。我有這些代碼:TCP/IP在兩臺計算機之間不工作

server.c

#include <sys/socket.h> 
#include <netinet/in.h> 
#include <arpa/inet.h> 
#include <stdio.h> 
#include <stdlib.h> 
#include <unistd.h> 
#include <errno.h> 
#include <string.h> 
#include <sys/types.h> 
#include<fcntl.h> 

int main(){ 
    int serverid, clientid, n = 0, len; 
    char msgsend[512], msgrecv[512]; 
    struct sockaddr_in server,client; 

    serverid = socket(AF_INET, SOCK_STREAM, 0); 
    if (serverid < 0) { 
    puts("Error creating socket\n"); 
    return -1; 
    } 

    memset(&server, 0, sizeof(server)); 
    memset(msgrecv, 0, sizeof(msgrecv)); 

    server.sin_family = AF_INET; 
    server.sin_addr.s_addr = htonl(INADDR_ANY); 
    server.sin_port = htons(5000); 

    if(bind(serverid, (struct sockaddr*)&server, sizeof(server)) < 0) { 
    printf("Error binding\n"); 
    return -1; 
    } 

    if(listen(serverid, 10) < 0){ 
    printf("ERROR"); 
    return -1; 
    } 
    len = sizeof(client); 
    clientid = accept(serverid, (struct sockaddr*)&client, &len); 
    if (clientid < 0) { 
    printf("Connection error\n"); 
    return -1; 
    } 
    puts("Connected to client"); 

    while(1){ 
    n = recv(clientid, msgrecv, sizeof(msgrecv) - 1, 0); 
    if(n < 0){ 
     printf("Error while reading...\n"); 
     return -1; 
    } 

    printf("client says:\n"); 
    msgrecv[n] = 0; 
    fputs(msgrecv, stdout); 
    printf("\n"); 
    printf("enter your reply\n"); 
    gets(msgsend); 
    send(clientid, msgsend, sizeof(msgsend) - 1, 0); 
    printf("\n"); 
    } 
    return 0; 
} 

client.c

#include <sys/socket.h> 
#include <netinet/in.h> 
#include <arpa/inet.h> 
#include <stdio.h> 
#include <stdlib.h> 
#include <unistd.h> 
#include <errno.h> 
#include <string.h> 
#include <sys/types.h> 
#include <fcntl.h> 

int main(){ 
    int sock, n = 0; 
    char msgsend[512], msgrecv[512]; 
    struct sockaddr_in server; 


    memset(&server, 0, sizeof(server)); 
    memset(msgrecv, 0, sizeof(msgrecv)); 

    sock = socket(AF_INET, SOCK_STREAM, 0); 
    if(sock == -1){ 
    printf("Could not create a socket!\n"); 
    return -1; 
    } 

    server.sin_family = AF_INET; 
    server.sin_addr.s_addr = inet_addr("192.168.112.130"); 
    server.sin_port = htons(5000); 


    printf(" Trying to connect...\n"); 

    if(connect(sock, (struct sockaddr*)&server, sizeof(server)) < 0){ 
    printf("Error:Connection failed\n"); 
    return -1; 
    } 
    printf("Connected\n"); 
    while(1){ 
    printf("please enter your reply\n"); 
    gets(msgsend); 

    send(sock, msgsend, sizeof(msgsend) - 1, 0); 
    n = recv(sock, msgrecv, sizeof(msgrecv) - 1, 0); 

    if(n < 0){ 
     printf("Read error\n"); 
    } 
    printf("\n"); 
    printf("server says:"); 
    msgrecv[n] = 0; 
    fputs(msgrecv, stdout); 
    printf("\n"); 
    } 
    return 0; 
} 

我運行第一./server然後在另一個終端./client。如果我使用同一臺計算機,它可以正常工作,但是如果我想在另一臺計算機和客戶端上使用服務器,它永遠不會連接。 我不知道我做錯了什麼。也許我必須使用另一個IP?外國電腦的代碼的一部分必須是不同的?

注意:一臺電腦是一個帶以太網的桌面,另一臺是通過WiFi連接到同一路由器的筆記本電腦。

+0

計算機上的操作系統是什麼?如果是Windows,則嘗試禁用防火牆,如果是linux,則在iptables上啓用該端口或其他任何防火牆。由於服務器和客戶端在同一臺機器上正常工作,可能問題在其他地方。 –

回答

0

調試這種東西的一般方法是嘗試在兩個方向ping,並看看它是否工作。如果沒有,則必須檢查路由表的配置以及設備上的接口以及路由器上的轉發/防火牆/ nat設置。確保之後雙方都能ping通。

如果這確實起作用,請測試您是否可以使用TCP一些簡單的程序如nc甚至您自己的服務器。如果ping工作正常,但TCP連接沒有,那麼它很可能是其中一臺主機(通常是服務器)的防火牆設置。調整設置然後再次測試。如果您不確定自己的應用程序是否有效,那麼您可能在此時使用了nc來測試連接,現在可以繼續測試您的應用程序。

相關問題