c语言的套接字编程无法远程工作

时间:2017-11-06 20:53:17

标签: c sockets winsock

我试图用graphics.h库用c语言写一个在线国际象棋游戏。 我知道它很老了,但我想学习游戏理论和插座的基础知识。

我用127.0.0.1尝试了它并且它有效。通信分别像国际象棋游戏一样完成,这就是为什么我写这样的代码。

问题是当我试图在另一台计算机上运行客户端程序时(在这种情况下,ip是我的计算机的ip。)它没有用。我已经停用防火墙,但它没有解决。我确定它在本地工作。

感谢您的帮助,如果我犯了错误,请原谅我。

这是我的server.c

#include<stdio.h>
#include<winsock2.h>
#pragma comment(lib,"ws2_32.lib") //Winsock Library
#define BUFLEN 512  //Max length of buffer
#define PORT 312   //The port on which to listen for incoming data         
int main(){
SOCKET s;
struct sockaddr_in server, si_other;
int slen , recv_len;
char buf[BUFLEN];
WSADATA wsa;
slen = sizeof(si_other) ;
//Initialise winsock
printf("\nInitialising Winsock...");
if (WSAStartup(MAKEWORD(2,2),&wsa) != 0){
    printf("Failed. Error Code : %d",WSAGetLastError());
    exit(EXIT_FAILURE);
}
printf("Initialised.\n");

//Create a socket
if((s = socket(AF_INET , SOCK_DGRAM , 0 )) == INVALID_SOCKET){
    printf("Could not create socket : %d" , WSAGetLastError());
}
printf("Socket created.\n");
//Prepare the sockaddr_in structure
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons( PORT );
//Bind
if( bind(s ,(struct sockaddr *)&server , sizeof(server)) == SOCKET_ERROR){
    printf("Bind failed with error code : %d" , WSAGetLastError());
    exit(EXIT_FAILURE);
}
puts("Bind done");
char message[500];
//keep listening for data
char msg[50];
int order=0;
while(1){
    memset(buf,'\0', BUFLEN);
    memset(msg,'\0',50);
    printf("Waiting for data...");
    fflush(stdout);
     if(order==1){
        gets(msg);
        sendto(s, msg, 20,0, (struct sockaddr*) &si_other, slen);           
     }
     if( recvfrom(s, buf, 20, 0, (struct sockaddr *) &si_other, &slen) &&       
     order==0){
        puts(buf);
        order=1;
     }
    printf("Received packet from %s:%d\n", inet_ntoa(si_other.sin_addr),
    ntohs(si_other.sin_port));
    printf("Data: %s\n" , buf);
}
closesocket(s);
WSACleanup();
return 0;
}

和client.c

#include<stdio.h>
#include<winsock2.h>
#pragma comment(lib,"ws2_32.lib") //Winsock Library
#define SERVER "127.0.0.1"  //ip address of udp server
#define BUFLEN 512  //Max length of buffer
#define PORT 312  //The port on which to listen for incoming data

int main(void){
struct sockaddr_in si_other;
int s, slen=sizeof(si_other);
char buf[BUFLEN];
char message[BUFLEN];
WSADATA wsa;
//Initialise winsock
printf("\nInitialising Winsock...");
if (WSAStartup(MAKEWORD(2,2),&wsa) != 0){
    printf("Failed. Error Code : %d",WSAGetLastError());
    exit(EXIT_FAILURE);
}
printf("Initialised.\n");
//create socket
if ( (s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == SOCKET_ERROR){
    printf("socket() failed with error code : %d" , WSAGetLastError());
    exit(EXIT_FAILURE);
}
//setup address structure
memset((char *) &si_other, 0, sizeof(si_other));
si_other.sin_family = AF_INET;
si_other.sin_port = htons(PORT);
si_other.sin_addr.S_un.S_addr = inet_addr(SERVER);
int order=1;
//start communication
while(1){
    printf("Enter message : ");
    gets(message);
    //send the message
    if (sendto(s, message,20 , 0 , (struct sockaddr *) &si_other, slen) 
    && order==1){
       order=0;
    }
   if(recvfrom(s, buf, 20, 0, (struct sockaddr *) &si_other, &slen) && 
   order==0){
        order=1;
        puts(buf);
    }
    memset(buf,'\0', BUFLEN);
    puts(buf);
}
closesocket(s);
WSACleanup();
return 0;
}

0 个答案:

没有答案