/******************server.c*************/
#include<stdio.h>
#include<strings.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#define PORT 1234
#define BACKLOG 1
#define MAXDATASIZE 100
struct ARG
{
int connfd;
struct sockaddr_in client;
};
struct RECEIVE
{
int connfd;
char *buf;
struct sockaddr_in client;
};
void *send_func(void *arg );
void *recive_func(void *arg );
int main()
{
pthread_t tid;
int listenfd, connectfd;
struct sockaddr_in server;
struct sockaddr_in client;
socklen_t addrlen;
char buf[MAXDATASIZE];
struct ARG *arg, in;
arg = ∈
struct RECEIVE *rec, recevie;
rec = &recevie;
if((listenfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
perror("socket() error.");
exit(1);
}
int opt = 1;
setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
bzero(&server, sizeof(server));
server.sin_family = AF_INET;
server.sin_port = htons(PORT);
server.sin_addr.s_addr = htonl(INADDR_ANY);
if(bind(listenfd, (struct sockaddr *)&server, sizeof(server)) == -1)
{
perror("bind() error.");
exit(1);
}
if(listen(listenfd, BACKLOG) == -1)
{
perror("listen() error.");
exit(1);
}
addrlen = sizeof(client);
while(1)//服务器程序永远不退出
{
printf("start while(1) /n");
if((connectfd = accept(listenfd, (struct sockaddr *)&client, &addrlen )) == -1)
{
perror("accept() error.");
exit(1);
}
printf("You got a connection from client ip is: %s,port is:%d/n",inet_ntoa(client.sin_addr), htons(client.sin_port));
//arg = (struct ARG *)malloc(sizeof(struct ARG));
arg->connfd = connectfd;
arg->client = client;
if(pthread_create(&tid, NULL, send_func, (void *)arg))
{
perror("error pthread_create");
exit(1);
}
printf("thread send id=%d/n",tid);
rec->buf=buf;
rec->connfd = connectfd;
rec->client = client;
if(pthread_create(&tid, NULL, recive_func, (void *)rec))
{
perror("error pthread_create");
exit(1);
}
printf("thread recieve id=%d/n",tid);
continue;
close(connectfd);
close(listenfd);
}
}
void *recive_func(void *rec )
{
struct RECEIVE *r;
int ret=0;
// r = (struct RECEIVE *)rec;
r=(struct RECEIVE *)malloc(sizeof(struct RECEIVE));
memcpy(r,(struct RECEIVE *)rec,sizeof(struct RECEIVE));
// 不能用 strncpy cqx
while(1)
{
printf("server start recv..... /n");
ret=recv(r->connfd, r->buf, MAXDATASIZE,0);
printf("recv ret =%d..... /n",ret);
if(ret<=0)
{
pthread_exit(NULL);
break;
}
printf("the client =%d..... /n",r->connfd);
printf("the client says: %s/n",r->buf);
}
}
void *send_func(void *arg)
{
char buf[MAXDATASIZE];
struct ARG *info;
//info = (struct ARG *)arg;
info=(struct ARG *)malloc(sizeof(struct ARG));
memcpy(info,(struct ARG *)arg,sizeof(struct ARG));
while(1)
{
fgets(buf, MAXDATASIZE, stdin);
#if 0
if(buf[0]%2 ==0)
info->connfd=4;
else
info->connfd=5;
#endif
//分析是法给哪一个客户
printf("info->connfd =%d ",info->connfd);
send(info->connfd, buf, MAXDATASIZE, 0);
}
}
-----------------------------------------------------------------------
//client.c///
#include<stdio.h>
#include<unistd.h>
#include<strings.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<netdb.h>
#define PORT 1234
#define MAXDATASIZE 100
void *func(void *arg);
void *func_get(void *arg);
char buf[MAXDATASIZE] ="123456789";
int main ( int argc, char *argv[])
{
pthread_t tid[2];
int sockfd, num;
// char buf[MAXDATASIZE] ="123456789";
struct hostent *he;
struct sockaddr_in server;
if(argc!=2)
{
printf("usage %s<ip address>/n",argv[0]);
exit(1);
}
if((he = gethostbyname(argv[1])) == NULL)
{
printf("gethostbyname error/n");
exit(1);
}
if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
printf("socket() error /n");
exit(1);
}
bzero(&server, sizeof(server));
server.sin_family = AF_INET;
server.sin_port = htons(PORT);
server.sin_addr = *((struct in_addr *)he->h_addr);
if(connect(sockfd, (struct sockaddr *)&server, sizeof(server)) == -1)
{
printf("connetc() error/n");
exit(1);
}
if(pthread_create(&tid[0], NULL, func, (void *)&sockfd))
{
perror("error pthread_create");
return 2;
}
if(pthread_create(&tid[0], NULL, func_get, (void *)&sockfd))
{
perror("error pthread_create");
return 2;
}
pthread_join(tid[0]);
pthread_join(tid[1]);
// close(sockfd);
}
void *func_get(void *arg)
{
int *sockfd ,num;
//char buf[MAXDATASIZE];
sockfd = (int *)arg;
while(1)
{
memset(buf,0,MAXDATASIZE);
printf("*sockefd=%d /n",*sockfd);
num= recv(*sockfd, buf, MAXDATASIZE, 0);
printf("num: %d/n",num);
if(0==num)// 返回0和-1都异常
{
pthread_exit(NULL);
// close(sockfd);
break;
}
printf("server message: %s/n",buf);
}
}
void *func(void *arg)
{
int *sockfd;
char buf_in[MAXDATASIZE];
sockfd = (int *)arg;
while(1)
{
fgets(buf_in, MAXDATASIZE, stdin);
send(*sockfd, buf_in, MAXDATASIZE, 0);
printf("func send/n");
}
}