c版多线程

来源:互联网 发布:plc编程入门ppt 编辑:程序博客网 时间:2024/06/07 17:36

MultiTuxedoListenerThread.c

 /* * MultiTuxedoListenerThread.c * *  Created on: 2011-12-9 *      Author: tuxedo */#include <pthread.h>#include <stdio.h>#include <stdlib.h>#include <sys/time.h>#include <string.h>#include <sys/types.h>#include <netinet/in.h>#include <sys/socket.h>#include <sys/wait.h>#include "atmi.h"#define SERVPORT 14109#define BACKLOG 10#define MAXSIZE 1024#define MAX 10pthread_t thread[1];//new thread be stored herepthread_mutex_t mut;int number = 0, i;typedef struct _CLIENT {int socket; //客户端socketchar ip[16]; //客户端IP地址} CLIENT;void *client_proc(void *arg) {//sleep(50);CLIENT client = *(CLIENT*) arg;int sock = client.socket;long sendlen;long rcvlen;int ret;char* rcvbuf;char* sendbuf;char requestCode[MAXSIZE] = { 0, };char requestBody[MAXSIZE] = { 0, };int rval;//char buf[MAXSIZE];/** * read data from socket client */char buf[MAXSIZE] = { 0, };      /**       * LOOP read data in client socket      **/while ((rval = read(sock, buf, MAXSIZE)) < 0) {printf("rval is %d buf is %s", rval, buf);perror("reading stream error!");continue;}printf("rval is %d buf is %s\n", rval, buf);sendlen = strlen(buf);for (i = 0; i < 6; i++) {requestCode[i] = buf[i]; //it is proccessed in cpu}for (i = 0; i < sendlen - 6; i++) {requestBody[i] = buf[i + 6];}requestBody[strlen(buf) - 6] = '\0';requestCode[strlen(buf)] = '\0';      (void) fprintf(stdout, "code: %s\n", requestCode);(void) fprintf(stdout, "body: %s\n", requestBody);if (tpinit((TPINIT *) NULL) == -1) {printf("failed init\n");(void) fprintf(stderr, "Tpinit failed\n");(void) fprintf(stderr, "Tperrno = %d [%s]\n", tperrno,tpstrerror(tperrno));printf("failed init\n");exit(1);}if ((sendbuf = (char *) tpalloc("STRING", NULL, sendlen + 10)) == NULL) {(void) fprintf(stderr, "Error allocating send buffer\n");tpterm();exit(1);}rcvlen = 1024;if ((rcvbuf = (char *) tpalloc("STRING", NULL, 1024)) == NULL) {(void) fprintf(stderr, "Error allocating receive buffer\n");tpfree(sendbuf);tpterm();exit(1);}(void) strcpy(sendbuf, requestBody);sendbuf[strlen(sendbuf)] = '\0';/** * send request to tuxedo server and receive response from tuxedo server */ret = tpcall(requestCode, (char *) sendbuf, 0, (char **) &rcvbuf, &rcvlen,(long) 0);if (ret == -1) {(void) fprintf(stderr, "Can't send request to service \n");(void) fprintf(stderr, "Tperrno = %d\n", tperrno);tpfree(sendbuf);tpfree(rcvbuf);tpterm();exit(1);}rcvbuf[strlen(rcvbuf)] = '!';rcvbuf[strlen(rcvbuf) + 1] = '\n';rcvbuf[strlen(rcvbuf) + 2] = '\0';(void) fprintf(stdout, "rcvbuf 3:%s\n", rcvbuf);//send response data from tuxedo server to socket clientif (send(sock, rcvbuf, strlen(rcvbuf), 0) == -1) {perror("send error!");printf("10\n");}tpfree(sendbuf);tpfree(rcvbuf);tpterm();close(sock);//exit(0);printf("IP_ADDRESS:%s, SOCKET:%d\n", client.ip, sock);}int main() {/** * socket configuration */int sockfd;int client_fd;struct sockaddr_in my_addr;struct sockaddr_in remote_addr;//创建套接字if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {perror("socket create failed!");exit(1);}//绑定端口地址my_addr.sin_family = AF_INET;my_addr.sin_port = htons(SERVPORT);my_addr.sin_addr.s_addr = INADDR_ANY;bzero(&(my_addr.sin_zero), 8);if (bind(sockfd, (struct sockaddr*) &my_addr, sizeof(struct sockaddr))== -1) {perror("bind error!");exit(1);}//监听端口if (listen(sockfd, BACKLOG) == -1) {perror("listen error");exit(1);}for (;;) {int sin_size = sizeof(struct sockaddr_in);client_fd = accept(sockfd, (struct sockaddr*) &remote_addr,(socklen_t*) &sin_size);if (client_fd == -1) {continue;}/*printf("Received a connection from %s\n", (char*) inet_ntoa(remote_addr.sin_addr));*/CLIENT client;client.socket = client_fd;strncpy(client.ip, (char *)inet_ntoa(remote_addr.sin_addr), 16);pthread_create(&thread[0], NULL, client_proc, (void*) &client); //这里传client给子线程}return 0;}


0 0
原创粉丝点击