LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 10-14-2012, 01:40 AM   #1
avijeetkumar
LQ Newbie
 
Registered: Oct 2012
Posts: 1

Rep: Reputation: Disabled
Socket Thread Programing


I am trying to synchronize a server for more than two different client using mutex..... but I am not getting the exact Idea.... Here is the server code...

Plz guide me regarding dat....

#include"header.h"
int server_sockfd, client_sockfd;
pthread_mutex_t mutex;

void *wwrite(void *ch)
{
pthread_mutex_lock(&mutex);
ch++;
write(client_sockfd, &ch, sizeof(ch));
pthread_mutex_unlock(&mutex);
}


int main()
{
int server_len, client_len;
struct sockaddr_un server_address;
struct sockaddr_un client_address;

pthread_t thread1;
/* remove old socket and create an unnamed socket */

unlink("server_socket");
server_sockfd = socket(AF_UNIX, SOCK_STREAM, 0);

/* Name the Socket */

server_address.sun_family = AF_UNIX;
strcpy(server_address.sun_path, "server_socket");
server_len = sizeof(server_address);
bind(server_sockfd, (struct sockaddr *)&server_address, server_len);

/* create a connection queue and wait for clients */

while(1)
{
listen(server_sockfd, 5);
printf("server waiting\n");




/* Accept connection */
int ch;
client_len = sizeof(client_address);
client_sockfd = accept(server_sockfd, (struct sockaddr *)&client_address, &client_len);


/* read and write to client on client_sockfd */

read(client_sockfd, &ch, sizeof(ch));
printf("you entered %d\n",ch);
pthread_create(&thread1,NULL,wwrite,(void *)ch);
pthread_join(thread1,NULL);
}
}
close(client_sockfd);


}
 
Old 10-14-2012, 03:43 AM   #2
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 8,527

Rep: Reputation: 2439Reputation: 2439Reputation: 2439Reputation: 2439Reputation: 2439Reputation: 2439Reputation: 2439Reputation: 2439Reputation: 2439Reputation: 2439Reputation: 2439
I do not really understand what do you want to implement. But first, please try to use [code][/code] to keep formatting of your code. There is a problem with { and/or }.
Using pthread_create together with pthread_join means you create a thread and wait until it finished, therefore this while cycle will never be able to run two or more threads parallel.
 
  


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
[SOLVED] problem with server-socket programing chandinisree Linux - Networking 10 08-23-2010 04:49 AM
socket programing question golden_boy615 Programming 1 02-28-2010 12:29 AM
socket programing - using message boundaries libinmeledam Linux - Networking 4 06-03-2008 07:48 PM
I need some socket programing in C help tomazN Programming 6 12-08-2005 12:35 PM
Raw Socket Programing djgerbavore Programming 2 08-18-2004 01:58 PM


All times are GMT -5. The time now is 06:54 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