LINUX C an socket programming
HI
I have to write a C program under linux that will act as a server and client at the same time so 2 users will run there program in console like this ./thenameofprogram IP_to_connect_to port_to_listen port_to_send_data So I wrote this program I used 2 threads but I have a problem when I start te 1 program it is normal for it that it cannot connect until the second program is not running but when I start the second program the 1 one connects to the second but then the second does not connect to first Code:
#include <unistd.h> can someone help me ??? I don't know what to do |
Is there anything dictating that the program has to work this way?
I would have the program work in more of a P2P manner. IE, on startup the program would try to connect and if it can't (seemingly because the other client isn't up yet) it will start listening. Maybe ever few seconds it might try and connect again, in case they both started up at the same time and both failed to connect to each other because they both were listening yet. Once there is a connection established, then the guy that had a success connect() could shut down his listening thread, and the other guy can stop attemping to connect() and you can use the one pipe for two way communications. Hopefully my explination wasn't too terribly confusing here... |
I wanted to to this program like you wrote but it must bee written like I explaned i the first post
|
Well, to do what you describe your going to have to keep trying to connect if connect returns a failure. I suggest checking the return value of connect, if it isn't 0, then sleep for a second and try again.
|
Moved: This thread is more suitable in Programming and has been moved accordingly to help your thread/question get the exposure it deserves.
|
Re: LINUX C an socket programming
Quote:
|
All times are GMT -5. The time now is 01:55 AM. |