客户端:
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <netdb.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <pthread.h>
#define PORT 4323
void *threadsend(void *arg)
{
int conn = *(int *)arg;
char temp[100];
while(1)
{
memset(temp, 0, sizeof(100));
fgets(temp, 100, stdin);
send(conn, temp, 100, 0);
printf("client ok\n");
}
}
void *threadrecv(void *arg)
{
int conn = *(int *)arg;
char temp[100];
while(1)
{
int ret;
memset(temp, 0, sizeof(100));
ret = (conn, temp, 100);
ret = recv(conn, temp, 100, 0);
if(ret > 0)
{
printf("server: %s",temp);
}
}
}
int main()
{
int clientfp;
struct sockaddr_in serveraddr;
clientfp = socket(AF_INET, SOCK_STREAM, 0);
if(clientfp < 0)
{
perror("socket error\n");
exit(1);
}
struct hostent *hp;
bzero(&serveraddr, sizeof(serveraddr));
serveraddr.sin_family = AF_INET;
serveraddr.sin_port = htons(PORT);
serveraddr.sin_addr.s_addr = inet_addr("127.0.0.1");
if(connect(clientfp, (struct sockaddr *)&serveraddr, sizeof(serveraddr)) > 0)
{
printf("connect error\n");
exit(1);
}
pthread_t tid1, tid2;
printf("connected\n");
pthread_create(&tid1, NULL, threadsend, &clientfp);
pthread_create(&tid2, NULL, threadrecv, &clientfp);
while(1);
close(clientfp);
return 0;
}
服务器:
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <netdb.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <pthread.h>
#define PORT 4323
#define MAX 1024
struct connarry
{
int member[MAX];
int len;
};
struct connarry connArry;
void *threadsend(void *arg)
{
// int conn = *(int *)arg;
char temp[100];
int i;
while(1)
{
memset(temp, 0 ,sizeof(temp));
fgets(temp, 100, stdin);
for(i = 0; i <= connArry.len; i++)
{
send(connArry.member[i], temp, 100, 0);
}
// send(conn, temp, 100, 0);
printf("server send ok\n");
}
}
void *threadrecv(void *arg)
{
int conn = *(int *)arg;
char temp[100];
char num[30];
memset(num, 0, sizeof(num));
sprintf(num, "+client:%d\n", conn);
printf("%s", num);
int i;
while(1)
{
memset(temp, 0, sizeof(temp));
int ret;
ret = read(conn, temp, 100);
// ret = recv(conn, temp, 100, 0);
if(ret > 0)
{
// printf("收到的倒数第一位是:%d----\n", ret);
strcat(temp, num);
printf("client: %s", temp);
for(i = 0; i <= connArry.len; i++)
{
if(connArry.member[i] != conn)
send(connArry.member[i], temp, 100, 0);
}
}
}
}
void *thread(void *arg)
{
pthread_t tid1,tid2;
int conn = *(int *)arg; //???
pthread_create(&tid1, NULL, threadsend, arg);
pthread_create(&tid2, NULL, threadrecv, arg);
// close(*(int *)arg);
}
int main()
{
int listenfd = socket(AF_INET, SOCK_STREAM, 0);
if(listenfd < 0)
{
perror("socket error");
exit(1);
}
struct hostent *hp; //???
struct sockaddr_in serveraddr;
bzero((char *)&serveraddr, sizeof(serveraddr));
serveraddr.sin_family = AF_INET;
serveraddr.sin_addr.s_addr = htonl(INADDR_ANY);
serveraddr.sin_port = htons(PORT);
if(bind(listenfd, (struct sockaddr *)&serveraddr, sizeof(serveraddr)) < 0 )
{
perror("bind error\n");
exit(1);
}
if(listen(listenfd, MAX) < 0)
{
perror("listen error");
exit(1);
}
struct sockaddr_in clientaddr;
int clientlen;
int conn;
clientlen = sizeof(clientaddr);
printf("server begin--------------\n");
connArry.len = -1;
while(1)
{
conn = accept(listenfd, (struct sockaddr *)&clientaddr, &clientlen);
connArry.len++;
connArry.member[connArry.len] = conn;
pthread_t tid;
printf("Accepted!\n");
pthread_create(&tid, NULL, thread, &conn);
}
close(listenfd);
return 0;
}