Advertisement

BIG NEED for help plzzz

Started by October 28, 2005 08:33 AM
2 comments, last by GameDev.net 19 years, 3 months ago
Hi. Im new in socket programming and sorry if my question is very stupid. I have the following problem. I dont have public IP so the guy on the server, to witch Im connected, redirected port number 880 to point to my computer. So when the client connect to the server on port 880 the server will redirect the client to connect to me and I'll receive from 192.168.0.1 port 80. I've copied a UDP server and client from another thread and changed it a little so that it will be compatible with Windows XP and winsock 2.0. When the the client sends something to the server, the server doesn't receive it and the client has error 10054.I DONT have any errors when sending but the server doesn't receive it. So here is the source: Server: #include "stdafx.h" #ifndef _WINSOCKAPI_ #define _WINSOCKAPI_ #endif #include <winsock2.h> #include <windows.h> #include <conio.h> #include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <string.h> #include <Winbase.h>//for Sleep() #define LOCAL_SERVER_PORT 80//Ive tried to write port 880 still have the same result #define MAX_MSG 20 int main(int argc, char *argv[]) { int rc, n, cliLen, flags; SOCKET sd; struct sockaddr_in cliAddr, servAddr; char msg[MAX_MSG]; WSADATA w; int error = WSAStartup (0x0002, &w); if (error) { printf("WSAStartup error"); getch(); return 0; } if (w.wVersion != 0x0002) { WSACleanup (); return 0; } /* socket creation */ sd=socket(AF_INET, SOCK_DGRAM, 0); if(sd<0) { printf("%s: cannot open socket \n",argv[0]); getch(); exit(1); } /* bind local server port */ servAddr.sin_family = AF_INET; servAddr.sin_addr.s_addr = htonl(INADDR_ANY); servAddr.sin_port = htons(LOCAL_SERVER_PORT); rc = bind (sd, (struct sockaddr *) &servAddr,sizeof(servAddr)); if(rc<0) { printf("%s: cannot bind port number %d \n", argv[0], LOCAL_SERVER_PORT); printf("%d", WSAGetLastError()); getch(); exit(1); } printf("%s: waiting for data on port UDP %u\n", argv[0],LOCAL_SERVER_PORT); flags = 0; while(1) { /* init buffer */ memset(msg,0x0,MAX_MSG); /* receive message */ cliLen = sizeof(cliAddr); n = recvfrom(sd, msg, MAX_MSG, flags, (struct sockaddr *) &cliAddr, &cliLen);//the server is waiting here and doesn't receive from the client if(n<0) { printf("%s: cannot receive data \n",argv[0]); continue; } /* print received message */ printf("%s: from %s:UDP%u : %s \n", argv[0],inet_ntoa(cliAddr.sin_addr), ntohs(cliAddr.sin_port),msg); Sleep(2000);//sleep for 2 sec sendto(sd,msg,n,flags,(struct sockaddr *)&cliAddr,cliLen); }/* end of server infinite loop */ getch(); return 0; } Client: #include "stdafx.h" #ifndef _WINSOCKAPI_ #define _WINSOCKAPI_ #endif #include <winsock2.h> #include <windows.h> #include <conio.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #define REMOTE_SERVER_PORT 880 #define MAX_MSG 20 int isReadable(SOCKET sd,int * error,int timeOut) { // milliseconds fd_set socketReadSet; FD_ZERO(&socketReadSet); FD_SET(sd,&socketReadSet); struct timeval tv; int i; if (timeOut) { tv.tv_sec = timeOut / 1000; tv.tv_usec = (timeOut % 1000) * 1000; } else { tv.tv_sec = 0; tv.tv_usec = 0; } // if i=select(sd+1,&socketReadSet,0,0,&tv); if (i == SOCKET_ERROR) { *error = 1; return 0; } // if *error = 0; return FD_ISSET(sd,&socketReadSet) != 0; } /* isReadable */ int main(int argc, char *argv[]) { int rc, i, n, echoLen, flags, error, timeOut; struct sockaddr_in cliAddr, remoteServAddr, echoServAddr; struct hostent *h; char msg[MAX_MSG]; SOCKET sd; /* check command line args */ if(argc<3) { printf("usage : %s <server> <data1> ... <dataN> \n", argv[0]); getch(); exit(1); } WSADATA w; int err = WSAStartup (0x0002, &w); if (err) { printf("WSAStartup error"); getch(); return 0; } if (w.wVersion != 0x0002) { WSACleanup (); return 0; } /* get server IP address (no check if input is IP address or DNS name */ h = gethostbyname(argv[1]); if(h==NULL) { printf("%s: unknown host '%s' \n", argv[0], argv[1]); exit(1); } printf("%s: sending data to '%s' (IP : %s) \n", argv[0], h->h_name, inet_ntoa(*(struct in_addr *)h->h_addr_list[0])); remoteServAddr.sin_family = h->h_addrtype; memcpy((char *) &remoteServAddr.sin_addr.s_addr, h->h_addr_list[0], h->h_length); remoteServAddr.sin_port = htons(REMOTE_SERVER_PORT); /* socket creation */ sd = socket(AF_INET,SOCK_DGRAM,0); if(sd<0) { printf("%s: cannot open socket \n",argv[0]); exit(1); } /* bind any port */ cliAddr.sin_family = AF_INET; cliAddr.sin_addr.s_addr = htonl(INADDR_ANY); cliAddr.sin_port = htons(0); rc = bind(sd, (struct sockaddr *) &cliAddr, sizeof(cliAddr)); if(rc==SOCKET_ERROR) { printf("%s: cannot bind port\n", argv[0]); getch(); exit(1); } Sleep(3000); flags = 0; timeOut = 100; // ms /* send data */ for(i=2;i<argc;i++) { //I DONT have any errors with sending but the server doesnt receive it rc = sendto(sd, argv, strlen(argv)+1, flags, (struct sockaddr *) &remoteServAddr, sizeof(remoteServAddr)); if(rc==SOCKET_ERROR) { printf("%s: cannot send data %d \n",argv[0],i-1); getch(); closesocket(sd); exit(1); printf("sended %d", rc); } /* init buffer */ memset(msg,0x0,MAX_MSG); while (!isReadable(sd,&error,timeOut)) printf("."); printf("\n"); /* receive echoed message */ echoLen = sizeof(echoServAddr); n = recvfrom(sd, msg, MAX_MSG, flags, (struct sockaddr *) &echoServAddr, &echoLen); if(n<0) {// here is the error printf("%s: cannot receive data \n",argv[0]); printf("%d",WSAGetLastError()); continue; } /* print received message */ printf("%s: echo from %s:UDP%u : %s \n", argv[0],inet_ntoa(echoServAddr.sin_addr), ntohs(echoServAddr.sin_port),msg); } return 1; }
No pain, no game.
Did you read the Forum FAQ section on NAT and firewalls?
enum Bool { True, False, FileNotFound };

This topic is closed to new replies.

Advertisement