1.创建线程‐‐pthread_create
int pthread_create( pthread_t *thread, //
线程
ID =
无符号长整型
const pthread_attr_t *attr, //
线程属性,
NULL
void *(*start_routine)(void *), //
线程处理函数
void *arg); //
线程处理函数
参数:
pthread:
传出参数,线程创建成功之后,会被设置一个合适的值
attr:
默认传
NULL
start_routine:
子线程的处理函数
arg:
回调函数的参数
返回值:
成功
:0
错误
:
错误号
//perror
不能使用该函数打印错误信息
主线程先退出,子线程会被强制结束
验证线程直接共享全局变量
client
#include<stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h> /* superset of previous */
#include<stdlib.h>
#include <strings.h>
#include<unistd.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
//./client 192.168.182.144 5001
#define SERV_PORT 5001
#define SERV_IP_ADDR "192.168.157.34"
#define BUFSIZE 1024
#define QUIT_STR "QUIT"
int main(int argc,char **argv)
{
int fd = -1;
if(argc!=3)
{
exit(1);
}
int port = -1;
port = atoi(argv[2]);
fd = socket(AF_INET,SOCK_STREAM,0);
if(fd<0)
{
perror("socket");
exit(1);
}
struct sockaddr_in sin;
bzero(&sin,sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_port = htons(port);
sin.sin_addr.s_addr = inet_addr(argv[1]);
if(connect(fd,(struct sockaddr*)&sin,sizeof(sin))<0)
{
perror("connect");
exit(1);
}
char buf[BUFSIZE];
while(1)
{
bzero(buf,BUFSIZE);
if(fgets(buf,BUFSIZE-1,stdin)==NULL)
{
continue;
}
write(fd,buf,strlen(buf));
if(!strncasecmp(buf,QUIT_STR,strlen(QUIT_STR)))
{
break;
}
}
return 0;
}
service
#include<stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h> /* superset of previous */
#include<stdlib.h>
#include <strings.h>
#include<unistd.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include<pthread.h>
#define SERV_PORT 5001
#define SERV_IP_ADDR "192.168.157.34"
#define BACKLOG 5
#define BUFSIZE 1024
#define QUIT_STR "QUIT"
void* client_data_handle(void* arg);
int main(int argc,char *argv[])
{
//socket
int fd = -1;
pthread_t tid;
fd = socket(AF_INET,SOCK_STREAM,0);
if(fd < 0)
{
perror("socket");
exit(1);
}
/*
struct sockaddr_in {
sa_family_t sin_family; address family: AF_INET
in_port_t sin_port; port in network byte order
struct in_addr sin_addr; internet address
};
Internet address.
struct in_addr {
uint32_t s_addr; address in network byte order
}; */
struct sockaddr_in sin;
bzero(&sin,sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_port = htons(SERV_PORT);
sin.sin_addr.s_addr = INADDR_ANY;
//bing
if(bind(fd,(struct sockaddr *)&sin,sizeof(sin))<0)
{
perror("bind");
exit(1);
}
//listen
if(listen(fd,BACKLOG)<0)
{
perror("listen");
exit(1);
}
int newfd = -1;
char ipv4_addr[16];
struct sockaddr_in clin;
socklen_t sockaddr_len = sizeof(clin);
while(1)
{
newfd = accept(fd,(struct sockaddr *)&clin,&sockaddr_len);
if(newfd<0)
{
perror("accept");
exit(1);
}
if(!inet_ntop(AF_INET,(void *)&clin.sin_addr,ipv4_addr,sizeof(clin)))
{
perror("inet_ntop");
exit(1);
}
printf("client :(%s,%d) is connect!\n",ipv4_addr,ntohs(clin.sin_port));
pthread_create(&tid,NULL,client_data_handle,(void *)&newfd);
}
close(fd);
//read
char buf[BUFSIZE];
int ret = -1;
while(1)
{
do{
bzero(buf,BUFSIZE);
ret = read(newfd,buf,BUFSIZE-1);
}while(ret<1);
if(ret<0)
{
perror("read");
exit(1);
}
if(!ret)
{
break;
}
printf("receive data :%s \n",buf);
if(!strncasecmp(buf,QUIT_STR,strlen(QUIT_STR)))
{
printf("Client is exiting!\n");
break;
}
}
}
void* client_data_handle(void* arg)
{
int newfd = *(int *)arg;
char buf[BUFSIZE];
int ret = -1;
printf("handle thread :newfd= %d\n",newfd);
while(1)
{
do{
bzero(buf,BUFSIZE);
ret = read(newfd,buf,BUFSIZE-1);
}while(ret<1);
if(ret<0)
{
perror("read");
exit(1);
}
if(!ret)
{
break;
}
printf("receive data :%s \n",buf);
if(!strncasecmp(buf,QUIT_STR,strlen(QUIT_STR)))
{
printf("Client is exiting!\n");
break;
}
}
close(newfd);
return NULL;
}
结果