|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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!!!!!!!!!! |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > C Programming > MAYDAY help about pthread!!!! |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|