LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-09-2005, 08:08 AM   #1
hasek
LQ Newbie
 
Registered: Jun 2005
Posts: 4

Rep: Reputation: 0
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
 
Old 06-09-2005, 08:19 AM   #2
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 67
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...
 
Old 06-09-2005, 08:39 AM   #3
hasek
LQ Newbie
 
Registered: Jun 2005
Posts: 4

Original Poster
Rep: Reputation: 0
I wanted to to this program like you wrote but it must bee written like I explaned i the first post
 
Old 06-09-2005, 09:49 AM   #4
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 67
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.
 
Old 06-12-2005, 09:59 AM   #5
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 79
Moved: This thread is more suitable in Programming and has been moved accordingly to help your thread/question get the exposure it deserves.
 
Old 06-12-2005, 10:34 AM   #6
freegianghu
Member
 
Registered: Oct 2004
Location: somewhere in the street
Distribution: Window$
Posts: 192

Rep: Reputation: 30
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
 
  


Reply



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
differ between linux and solaris in socket programming taureanyang Programming 2 07-06-2005 03:27 AM
Linux Socket Programming source code saied Programming 4 05-24-2005 02:18 AM
socket programming in linux ray5_83 Programming 4 11-18-2004 01:06 AM
Linux Socket Programming velan Programming 4 06-24-2004 04:03 AM
Socket Programming perdesiz Linux - Software 2 10-06-2003 12:15 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:56 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
Open Source Consulting | Domain Registration