c版多线程

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 10
pthread_t thread[1];//new thread be stored here
pthread_mutex_t mut;
int number = 0, i;

typedef struct _CLIENT {
	int socket; //客户端socket
	char 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 client
	if (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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值