C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old June 8th, 2003, 11:12 AM
PCPC PCPC is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2003
Location: turkey
Posts: 15 PCPC User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 28 m 49 sec
Reputation Power: 0
MAYDAY help about pthread!!!!

hi.i am preparing a project about linux sockets.iam writing a server.iwanna use pthread functions in server code.can anybody help me about usage of pthread funcs .any sample code.i know this functions but dunno how to use.i hope i could explain my problem.

these r the client and server codes.one or more clients will connect to server.there for i have to use pthread functions.i know nothing about them.
i wish u send me some information or samples about them.

CLIENT.C

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/wait.h>
#include <signal.h>

#define PORT 3490
#define MAXDATASIZE 100

int main(int argc, char *argv[])
{
int sockfd, numbytes,a;
char buf[MAXDATASIZE];
char msg[30];
struct hostent *he;
struct sockaddr_in their_addr;

if (argc !=2) {
fprintf(stderr,"usage: client hostname\n");
exit(1);
}

if((he = gethostbyname(argv[1])) == NULL) {
perror("gethostbyname");
exit(1);
}

if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror("socket");
exit(1);
}

their_addr.sin_family = AF_INET; // host byte order
their_addr.sin_port = htons(PORT); // short, network byte order
their_addr.sin_addr = *((struct in_addr *)he->h_addr);
memset(&(their_addr.sin_zero), '\0', 8);

if (connect(sockfd, (struct sockaddr *)&their_addr, sizeof(struct sockaddr)) < 0) {
perror("connect");
exit(1);
}


printf("Print your message:");
gets(msg);
if(send(sockfd,msg,strlen(msg),0) < 0) {
perror("send");
exit(1);
}

if(recv(sockfd,msg,30,0) < 0) {
perror("recv");
exit(1);
}
puts(msg);


close(sockfd);
return 0;
}



SERVER.C

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/wait.h>
#include <signal.h>

#define MYPORT 3490
#define BACKLOG 10

void sigchld_handler(int s)
{
while(wait(NULL) > 0);
}

int main(void)
{
int sockfd, newfd;
struct sockaddr_in my_addr, their_addr;
int sin_size;
struct sigaction sa;
int yes=1;
char msg[30];

if ((sockfd=socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror("socket");
exit(1);
}

my_addr.sin_family = AF_INET; // host byte order
my_addr.sin_port = htons(MYPORT); // short, network byte order
my_addr.sin_addr.s_addr = INADDR_ANY;
memset(&(my_addr.sin_zero), '\0', 8);

if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) < 0) {
perror("bind");
exit(1);
}

if (listen(sockfd, BACKLOG) < 0) {
perror("listen");
exit(1);
}

sa.sa_handler = sigchld_handler; //reap all dead processes
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_RESTART;
if (sigaction(SIGCHLD, &sa, NULL) < 0) {
perror("sigaction");
exit(1);
}

while(1) { //main accept() loop
sin_size = sizeof(struct sockaddr_in);
if((newfd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size)) <0) {
//perror("accept");
continue;
}
printf("server: got connection from %s\n", inet_ntoa(their_addr.sin_addr));

if(!fork()) {
if(recv(newfd,msg,30,0) < 0) {
perror("recv");
exit(1);
}
printf("Message received");
puts(msg);

if(send(newfd,"Message sent",14,0) < 0 ) {
perror("send");
exit(1);
}
}

}
return 0;
}


!!!!!!!!INSTEAD OF FORK I WNAT TO USE PTHREAD FUNCTIONS!!!!!!!!!!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > MAYDAY help about pthread!!!!


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
Stay green...Green IT