您的位置:首页 > 理论基础 > 计算机网络

多线程编程示例(基于tcp_socket)

2014-03-08 21:12 302 查看
服务端(server)/*************************************************************************
> File Name: threadserver.c
> Author: kid
> Mail: 280197326@qq.com
> Created Time: 2014年03月01日 星期六 23时20分20秒
************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <linux/shm.h>

#define MYPORT 3490 /* 监听端口 */
#define BACKLOG 10 /* listen的请求接收队列长度 */
#define LISTLENGTH 32
#define MAXDATASIZE 100 /*一次可以读的最大字节数 */

struct ip_list
{
int count;
int ip_fd[LISTLENGTH];
};

struct ip_list *viraddr;

void * trd(int new_fd)
{
int numbytes,i;
char buf[MAXDATASIZE];
while(1)
{
numbytes = recv(new_fd,buf,MAXDATASIZE,0);
buf[numbytes] = '\0';
if(numbytes==0)
{
printf("tid: %d close connect!\n", new_fd);
viraddr->ip_fd[new_fd]=0;
break;
}
printf("Received: %s",buf);
for(i=0;i<LISTLENGTH;i++)
{
if(viraddr->ip_fd[i]!=0)
{
send(viraddr->ip_fd[i], buf, MAXDATASIZE, 0);
printf("ip_fd:%d\n",viraddr->ip_fd[i]);
}
}
}
pthread_exit();
}

void main()
{
int sockfd, new_fd, numbytes, i=0; /* 监听端口,数据端口 */
char buf[MAXDATASIZE];
struct sockaddr_in my_addr; /* 自身的地址信息 */
struct sockaddr_in their_addr; /* 连接对方的地址信息 */
int sin_size,pid;

pthread_t t1;

viraddr = (struct ip_list *)malloc(sizeof(struct ip_list));
viraddr->count = 0;
for(i=0;i<LISTLENGTH;i++)
{
viraddr->ip_fd[i]=0;
}

sockfd = socket(AF_INET, SOCK_STREAM, 0);
my_addr.sin_family = AF_INET;
my_addr.sin_port = htons(MYPORT); /* 网络字节顺序 */
my_addr.sin_addr.s_addr = INADDR_ANY; /* 自动填本机IP */
bzero(&(my_addr.sin_zero), 8); /* 其余部分置0 */

bind(sockfd, (struct sockaddr *)&my_addr,sizeof(struct sockaddr));
listen(sockfd, BACKLOG);

while(1){ /* 主循环 */
sin_size = sizeof(struct sockaddr_in);
new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size);
// i=viraddr->count;
viraddr->ip_fd[new_fd]=new_fd;
viraddr->count = viraddr->count +1;
printf("Got connection from %s\n", inet_ntoa(their_addr.sin_addr));

pthread_create(&t1, NULL, trd, new_fd);
}
}
客户端(client)/*************************************************************************
> File Name: threadclient.c
> Author: kid
> Mail: 280197326@qq.com
> Created Time: 2014年03月01日 星期六 23时25分41秒
************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>

#define PORT 3490 /* Server的端口 */
#define MAXDATASIZE 100 /*一次可以读的最大字节数 */

int main(int argc, char *argv[])
{
int sockfd, numbytes,pid;
char buf[MAXDATASIZE];
struct hostent *he; /* 主机信息 */
struct sockaddr_in their_addr; /* 对方地址信息 */

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

sockfd=socket(AF_INET,SOCK_STREAM,0);
their_addr.sin_family = AF_INET;
their_addr.sin_port = htons(PORT); /* short, NBO */
their_addr.sin_addr = *((struct in_addr *)he->h_addr);
bzero(&(their_addr.sin_zero), 8); /* 其余部分设成0 */
connect(sockfd, (struct sockaddr *)&their_addr, sizeof(struct sockaddr));

while((pid=fork())==-1);
if(pid==0)
{
while(1)
{
numbytes = recv(sockfd,buf,MAXDATASIZE,0);
buf[numbytes] = '\0';
if(numbytes==0)
{
printf("connect close:%s\n",inet_ntoa(their_addr.sin_addr));
break;
}
printf("Received: %s\n",buf);
}
}else{
while(1)
{
// printf("your message:");
fgets(buf,MAXDATASIZE,stdin);
if(strncmp(buf,"#end#",5)==0)
break;
send(sockfd, buf, MAXDATASIZE, 0);
}
kill(pid,9);
}
close(sockfd);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: