Reputation: 1191
i am making some experiments with sockets in C but i have a problem that i think there is a solution but i can't figure it out!
i have a server and a client written in C and they works so good in local network, i tried 'localhost' and 127.0.0.1 or just 192.168.1.2 and everything works like a charm... but when i try to connect to a remote host (for my experiments i used my own computer with IP found on http://www.whatismyip.com/ 62.98.XXX.XX) connection and data (string) exchange never happens...
how can i solve this issue?
here is my server.c code
//server
#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 <time.h>
int main(int argc, char *argv[])
{
int listenfd = 0, connfd = 0;
struct sockaddr_in serv_addr;
char sendBuff[1025];
time_t ticks;
listenfd = socket(AF_INET, SOCK_STREAM, 0);
memset(&serv_addr, '0', sizeof(serv_addr));
memset(sendBuff, '0', sizeof(sendBuff));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr.sin_port = htons(5000);
bind(listenfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr));
listen(listenfd, 10);
while(1)
{
connfd = accept(listenfd, (struct sockaddr*)NULL, NULL);
ticks = time(NULL);
snprintf(sendBuff, sizeof(sendBuff), "%.24s\r\n", ctime(&ticks));
write(connfd, sendBuff, strlen(sendBuff));
close(connfd);
sleep(1);
}
}
and this is my client.c code
//client
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <arpa/inet.h>
int main(int argc, char *argv[])
{
int sockfd = 0, n = 0;
char recvBuff[1024];
struct sockaddr_in serv_addr;
if(argc != 2)
{
printf("\n Usage: %s <ip of server> \n",argv[0]);
return 1;
}
memset(recvBuff, '0',sizeof(recvBuff));
if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
printf("\n Error : Could not create socket \n");
return 1;
}
memset(&serv_addr, '0', sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(5000);
if(inet_pton(AF_INET, argv[1], &serv_addr.sin_addr)<=0)
{
printf("\n inet_pton error occured\n");
return 1;
}
if( connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
{
printf("\n Error : Connect Failed \n");
return 1;
}
while ( (n = read(sockfd, recvBuff, sizeof(recvBuff)-1)) > 0)
{
recvBuff[n] = 0;
if(fputs(recvBuff, stdout) == EOF)
{
printf("\n Error : Fputs error\n");
}
}
if(n < 0)
{
printf("\n Read error \n");
}
return 0;
}
Upvotes: 2
Views: 4375
Reputation: 91017
As some peopl in the comments already said, probably your router or whatever serves you to cross the boundary between 192.168.*
and 62.98.XXX.XX
blocks that access. The reason is clear - it doesn't know which of the PCs behind it is supposed to be conencted, if any.
You have to explicitly tell the box (I assume you have full access to it) to which internal IP to forward. If you don't have full access, you won't be able to do this, as there are no ports which are surely open.
Over 3G, it probably won't work, as most mobile providers give out IPv4 addresses in the range 10.0.0.0/8
.
Alternatively, as we have 2012 meanwhile, you could set up a IPv6 environment and modify your program to be capable of it.
Upvotes: 0
Reputation: 3825
At least one problem with this code:
memset(&serv_addr, '0', sizeof(serv_addr));
Do you really want to fill memory with 0x30?
Upvotes: 4