ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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>
#include <pthread.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <netdb.h>
#include <stdio.h>
#include <netinet/in.h>
int socketfd;//klient
int clientfd;
char*ena;
char*dva;
char*tri;
int serversocket;
struct sockaddr_in my_addr;
struct sockaddr_in server;
struct hostent *host;
int j=0;
int streznik1=0;
int klient1=0;
void*streznik(void*args)
{
serversocket = socket(AF_INET, SOCK_STREAM, 0);
my_addr.sin_family = AF_INET;
my_addr.sin_port = htons(atoi(dva));
my_addr.sin_addr.s_addr = inet_addr("0.0.0.0");
bzero(&(my_addr.sin_zero), 8);
if (bind(serversocket, (struct sockaddr *)&my_addr, sizeof(struct sockaddr))
==-1) {
perror("Cannot bind");
}
listen(serversocket, 10);
while(streznik1!=1)
{
// .....
struct sockaddr client_addr;
socklen_t size = sizeof(client_addr);
clientfd = accept(serversocket, &client_addr, &size);
if(clientfd!=-1)
{
printf("uspel sem dobiti povezavo na strezniku\n");
j++;
streznik1=1;
}//else{close(socketfd);}
}//konec while zanke
pthread_exit(0);
}
void*klient(void*args)
{
socketfd = socket(PF_INET, SOCK_STREAM, 0);
if (socketfd<0) {
perror("Error opening socket");
return -1;
}
server.sin_family= AF_INET;
server.sin_port = htons(atoi(tri));
host = gethostbyname(ena);
if (host==NULL) {
perror("Unknown host");
return -1;
}
while(klient1!=1)
{
server.sin_addr = *((struct in_addr*)host->h_addr);
if (connect(socketfd, (struct sockaddr*)&server, sizeof(struct sockaddr))== -1) {
perror("Connect");
//return -1;
//close(socketfd);
}else
{
printf("uspelo se mi je povezati z klientom\n");
j++;
klient1=1;
}
}
pthread_exit(0);
}
int main(int argc, char**argv)
{
if(argc!=4)
{
printf("premalo argumentov");
return -1;
}
pthread_t tid[2]; //polje niti
ena=argv[1];
dva=argv[2];
tri=argv[3];
//while(j!=2){
pthread_create(&tid[1],NULL,klient,0);
pthread_create(&tid[2],NULL,streznik,0);
while(j!=2){}
int i=0;
for(i=2;i>=0;i--){pthread_join(tid[i],NULL);}
close(socketfd);
close( clientfd);
return 0;
}
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...
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.