LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   LINUX C an socket programming (https://www.linuxquestions.org/questions/programming-9/linux-c-an-socket-programming-331837/)

hasek 06-09-2005 08:08 AM

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>
#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;
 }


can someone help me ??? I don't know what to do

jtshaw 06-09-2005 08:19 AM

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...

hasek 06-09-2005 08:39 AM

I wanted to to this program like you wrote but it must bee written like I explaned i the first post

jtshaw 06-09-2005 09:49 AM

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.

david_ross 06-12-2005 09:59 AM

Moved: This thread is more suitable in Programming and has been moved accordingly to help your thread/question get the exposure it deserves.

freegianghu 06-12-2005 10:34 AM

Re: LINUX C an socket programming
 
Quote:

Originally posted by hasek
HI
Code:



.........................

 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);}
// oops: tid[2]
 
  close(socketfd);
  close( clientfd);
 return 0;
 }


can someone help me ??? I don't know what to do



All times are GMT -5. The time now is 10:05 PM.