LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-05-2013, 11:47 AM   #1
sumanta_009
LQ Newbie
 
Registered: Mar 2009
Posts: 2

Rep: Reputation: 0
problem in socket programming


I was trying to make a chatting program.
There is a server and a client code within the same program. I don't know, whether this is possible.

The thought behind is,
when the program is called from a terminal without any argument, the server will be called first (listening on port 2222). When it receives a connect request it accepts and it will extract the IP of the requesting client. It will call its client thread using that IP and port (=2222+10).

At the other terminal, the program was called using the destination IP as argument, in this case the client thread will be called first with the IP and port (=2222). After that the server is called (listening at port=2222+10).

But I'm facing some problem, like the 'connect' returns error code-> unreachable network.

Still I'm not successful to develop this. Can you give me some better idea to develop a chatting program.
 
Old 09-06-2013, 12:34 PM   #2
linosaurusroot
Member
 
Registered: Oct 2012
Distribution: OpenSuSE,RHEL,Fedora,OpenBSD
Posts: 981
Blog Entries: 2

Rep: Reputation: 235Reputation: 235Reputation: 235
This sounds like a subject for the Programming forum.

Quote:
'connect' returns error code-> unreachable network
Can you connect to the appropriate destination using telnet or netcat, or reach it with ping or traceroute?

Can you check with netstat or tcpdump that the correct destination address is being used (and has not been missed because of the wrong byte order for example)?
 
Old 09-07-2013, 04:19 AM   #3
sumanta_009
LQ Newbie
 
Registered: Mar 2009
Posts: 2

Original Poster
Rep: Reputation: 0
check the code

when i am running one with argument 127.0.0.1 it is communicating with the other(server) at port 2222.
But the reverse (from the 2nd without argument which tries to connect to the first server component)is not working. it says "connection refused". I'm doing this in a same computer.

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<pthread.h>
#include<string.h>
#include<signal.h>

#define SERVER_PORT 2222
#define SERVER_PORT1 3333

int arg;
char client_ip[40];
void* client(void *stream)
{
int sock,n;
struct sockaddr_in server;
char message[1024] ,server_ip[16],*cp;


sock = socket(AF_INET , SOCK_STREAM , 0);
if (sock == -1)
{
printf("Could not create socket");
exit(0);
}
puts("Client:Socket created");

//fscanf((FILE *)stream,"%s",server_ip);
puts(server_ip);
server.sin_addr.s_addr = inet_addr(client_ip);
server.sin_family = AF_INET;
if(arg==2)
n=SERVER_PORT;
else
n=SERVER_PORT1;
server.sin_port = htons(n);
printf("trying to get port %i",n);
//Request for a connection to the server
if (connect(sock , (struct sockaddr *)&server , sizeof(server)) < 0)
{
perror("Error: ");
return NULL;
}

puts("Connected\n Now ");
//keep communicating with server
while(1)
{
printf("Enter message : ");
cp=fgets(message,sizeof(message),stdin);

n=strcmp("quit",message);
if(n==0)
{
printf("\nClient quit.\n");
break;
}

//Send some data
if( send(sock , message , strlen(message) , 0) < 0)
puts("Send failed");


memset(message,0,1024);

}

close(sock);
return NULL;
}


int main(int argc,char *argv[])
{
struct sockaddr_in server_addr,client_addr;
int host_sock_fd,client_sock_fd,shr_file_fd[2],n;
char client_message[1024];
FILE *cli_msg,*stream_read,*stream_write;
pthread_t srv_th_id,cli_th_id;

arg=argc;




host_sock_fd=socket(AF_INET,SOCK_STREAM,0);
puts("Server:Socket created");
server_addr.sin_family=AF_INET;
if(argc==2)
n=SERVER_PORT1;
else
n=SERVER_PORT;
server_addr.sin_port = htons(n);
server_addr.sin_addr.s_addr=INADDR_ANY;

if(0>bind(host_sock_fd,(struct sockaddr *)&server_addr,sizeof(server_addr)))
{
perror("error: ");
return 1;
}
//**when client started first**//
if(argc==2)
{
strcpy(client_ip,argv[1]);

pthread_create(&cli_th_id,NULL,&client,&stream_read);//client thread called
puts("\n\tClient thread called.");
sleep(1);
pthread_join(cli_th_id,NULL);
}
printf("\nbind done \n\tlistening at port %i.....",n);

listen(host_sock_fd,2);

int read_size;
n=sizeof(server_addr);
if((client_sock_fd=accept(host_sock_fd,(struct sockaddr *)&client_addr,(socklen_t*)&n))<0)
{
perror("error:");
return 1;
}

cli_msg=fopen("message.txt","w");

//**when server started first**//
if(argc==1)
{

strcpy(client_ip,inet_ntoa(client_addr.sin_addr));// Client IP

pthread_create(&cli_th_id,NULL,&client,NULL);
puts("\n\tClient thread called.");
sleep(1);
pthread_join(cli_th_id,NULL);
}

while( (read_size = recv(client_sock_fd , client_message , 1024 , 0)) > 0 )
{
sleep(1);
//strcat(client_message,"\0");
n=strcmp("quit",client_message);
if(n==0)
{
printf("\nClient wish to QUIT !!!\n");
break;
}
else
{
fprintf(cli_msg,"\n%s",client_message);//all received strings are saved.
printf("\n\nclient_message: %s",client_message);//////change
// sleep(1);
memset(client_message,' ',strlen(client_message));
}
// sleep(1);
}

if(read_size == 0)
{
puts("Client disconnected");
fflush(stdout);
return 1;
}
else if(read_size == -1)
{
perror("recv failed");
return 1;
}

close((unsigned int)cli_msg);
close((unsigned int)stream_read);
close((unsigned int)stream_write);

return 0;
}
 
Old 09-07-2013, 04:31 AM   #4
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,654

Rep: Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255
Quote:
Originally Posted by sumanta_009 View Post
I was trying to make a chatting program.
There is a server and a client code within the same program. I don't know, whether this is possible.

The thought behind is,
when the program is called from a terminal without any argument, the server will be called first (listening on port 2222). When it receives a connect request it accepts and it will extract the IP of the requesting client. It will call its client thread using that IP and port (=2222+10).
So what happens if two users on the same system try to run the application... The second one will not be able to open port 2222 or 2222+10.
[quote]
At the other terminal, the program was called using the destination IP as argument, in this case the client thread will be called first with the IP and port (=2222). After that the server is called (listening at port=2222+10).

Usually, a client will connect to a shared resource (the server), and all clients make only a single bi-directional connection. No need for multiple servers... which is why chat "rooms" are added so that a central server can route data to the correct list of connections. But I can understand wanting to host a local session, but normally that would only involve one user (the one starting the session) to start a server; everyone else would join that session by making a simple TCP connection to the designated host.

Quote:
But I'm facing some problem, like the 'connect' returns error code-> unreachable network.

Still I'm not successful to develop this. Can you give me some better idea to develop a chatting program.
Might take a bit - is this just an experiment? A full implementation would likely need something for logging in/authorizing connections, encrypted connections, why the need for using TCP (reliability), or maybe using UDP (simpler), including video/audio?...

The reason for avoiding multiple servers is to reduce the load. A n-person chat as you describe it would involve n servers - each having to have n connections to everybody... making it a bit unmanageable. And a bit slow - the overall conversation would be limited to the speed of the slowest connection (since everybody would be bombarding that one server, vs one server sending the consolidated data at one time; and that one server would have replicate its message to everyone...)
 
Old 09-08-2013, 09:37 AM   #5
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,654

Rep: Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255
It would make things clearer (with indentation protected) to put code blocks in [ code ] [ / code ] blocks (without the extra spaces in the sequence)...
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
hi please help me - problem in socket programming shakthi Programming 2 05-11-2011 11:02 PM
I/O problem in socket programming goldeneagle1234 Linux - Networking 1 06-19-2009 12:24 PM
Socket Programming Problem htabesh Programming 8 06-04-2009 01:28 PM
Socket Programming problem lucky6969b Programming 4 12-27-2005 01:44 AM
Socket programming problem krivi Programming 3 01-14-2002 03:04 AM


All times are GMT -5. The time now is 09:07 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration