实现TCP聊天室本质上就是用内核链表管理多线程,再通过遍历的方式向每一个客户端发送消息的过程。
server端(服务端代码实现)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <pthread.h>
#include "list.h"
#include <semaphore.h>
sem_t sem;
struct cli_t{
pthread_t tid; //创建线程的tid
struct sockaddr_in addr; //源地址的结构体/client端
int fd; //用于通信的不同的fd
char buf[50];
int len;
struct list_head lists; //内核链表
};
void *routine (void *arg)
{
//printf("2\n");
struct cli_t *p = NULL;
//printf("3\n");
struct list_head *ptr = NULL;
struct list_head *head = (struct list_head *)arg;
list_for_each(ptr,head)
{
p = list_entry(ptr,struct cli_t,lists);
if(pthread_equal(p->tid,pthread_self()) != 0) //判断线程号是否相等
{
break;
}
}
printf("p-> tid = %ld,self = %ld\n",p->tid,pthread_self());
//
while(1)
{
p->len = read(p->fd,p->buf,sizeof(p->buf));
printf("read is %s\n",p->buf);
if(p->len > 0)
{
list_for_each( ptr, head )
{
struct cli_t *c = list_entry( ptr, struct cli_t, lists );
write(c->fd,p->buf,p->len);
}
memset( p->buf, 0, sizeof(p->buf) );
p->len = 0;
}
}
}
int main()
{
sem_init(&sem,0,0);
int sock = socket( AF_INET, SOCK_STREAM, 0 );
LIST_HEAD( head );
//struct list_head head;
int opt = 1;
setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
struct sockaddr_in local;
local.sin_family = AF_INET;
local.sin_port = htons( 8899 );
local.sin_addr.s_addr = inet_addr( "127.0.0.1" );
int ret = bind( sock, (struct sockaddr *)&local, sizeof( local ));
if( ret < 0 ){
perror("bind error");
exit( 0 );
}
listen( sock, 10 );
printf("ALL READY\n");
while(1)
{
struct cli_t *c = malloc( sizeof(struct cli_t ) ); //每来一个客户端就创建一个结构体,内的数据
int len = sizeof(c->len);
c->fd = accept(sock,(struct sockaddr *)&c->addr,&len);
int ret = pthread_create(&c->tid,NULL,routine,(void *)&head); //为每一个客户端创建一个线程
if(ret < 0)
{
perror("create error");
exit(0);
}
printf("There is a new connection and his fd is %d\n",c->fd);
list_add(&c->lists,&head);
// printf("1\n");
sem_post(&sem);
}
return 0;
}
client端(客户端代码实现)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <pthread.h>
void *routine(void *arg)
{
int sock = (int)arg;
while(1)
{
char buf[50] = {0};
int ret = read(0,buf,sizeof(buf));
write(sock,buf,ret);
}
}
int main()
{
int sock = socket( AF_INET, SOCK_STREAM, 0 );
struct sockaddr_in ser;
ser.sin_family = AF_INET;
ser.sin_port = htons(8899);
ser.sin_addr.s_addr = inet_addr("127.0.0.1");
int ret = connect( sock, (struct sockaddr *)&ser, sizeof(ser) );
if( ret < 0 ){
perror("connect\n");
exit(0);
}
pthread_t tha;
pthread_create(&tha,NULL,routine,(void *)sock);
while( 1 ){
char buf[20] = {0};
int rec = read( sock, buf, sizeof(buf) );
printf("read server = %s\n", buf );
}
return 0;
}
内核链表函数库(调用更方便)
#ifndef _LINUX_LIST_H
#define _LINUX_LIST_H
struct list_head{
struct list_head *next, *prev;
};
#define LIST_POISON1 ((void *) 0x00100100)
#define LIST_POISON2 ((void *) 0x00200200)
#define container_of( ptr, type, mem ) \
(type*)((char *)ptr - (unsigned long)&((type*)0)->mem )
/*
* Simple doubly linked list implementation.
*
* Some of the internal functions ("__xxx") are useful when
* manipulating whole lists rather than single entries, as
* sometimes we already know the next/prev entries and we can
* generate better code by using them directly rather than
* using the generic single-entry routines.
*/
#define LIST_HEAD_INIT(name) { &(name), &(name) }
#define LIST_HEAD(name) \
struct list_head name = LIST_HEAD_INIT(name)
static inline void INIT_LIST_HEAD(struct list_head *list)
{
list->next = list;
list->prev = list;
}
/*
* Insert a new entry between two known consecutive entries.
*
* This is only for internal list manipulation where we know
* the prev/next entries already!
*/
#ifndef CONFIG_DEBUG_LIST
static inline void __list_add(struct list_head *new,
struct list_head *prev,
struct list_head *next)
{
next->prev = new;
new->next = next;
new->prev = prev;
prev->next = new;
}
#else
extern void __list_add(struct list_head *new,
struct list_head *prev,
struct list_head *next);
#endif
/**
* list_add - add a new entry
* @new: new entry to be added
* @head: list head to add it after
*
* Insert a new entry after the specified head.
* This is good for implementing stacks.
*/
static inline void list_add(struct list_head *new, struct list_head *head)
{
__list_add(new, head, head->next);
}
/**
* list_add_tail - add a new entry
* @new: new entry to be added
* @head: list head to add it before
*
* Insert a new entry before the specified head.
* This is useful for implementing queues.
*/
static inline void list_add_tail(struct list_head *new, struct list_head *head)
{
__list_add(new, head->prev, head);
}
/*
* Delete a list entry by making the prev/next entries
* point to each other.
*
* This is only for internal list manipulation where we know
* the prev/next entries already!
*/
static inline void __list_del(struct list_head * prev, struct list_head * next)
{
next->prev = prev;
prev->next = next;
}
/**
* list_del - deletes entry from list.
* @entry: the element to delete from the list.
* Note: list_empty() on entry does not return true after this, the entry is
* in an undefined state.
*/
#ifndef CONFIG_DEBUG_LIST
static inline void list_del(struct list_head *entry)
{
__list_del(entry->prev, entry->next);
entry->next = LIST_POISON1;
entry->prev = LIST_POISON2;
}
#else
extern void list_del(struct list_head *entry);
#endif
/**
* list_replace - replace old entry by new one
* @old : the element to be replaced
* @new : the new element to insert
*
* If @old was empty, it will be overwritten.
*/
static inline void list_replace(struct list_head *old,
struct list_head *new)
{
new->next = old->next;
new->next->prev = new;
new->prev = old->prev;
new->prev->next = new;
}
static inline void list_replace_init(struct list_head *old,
struct list_head *new)
{
list_replace(old, new);
INIT_LIST_HEAD(old);
}
/**
* list_del_init - deletes entry from list and reinitialize it.
* @entry: the element to delete from the list.
*/
static inline void list_del_init(struct list_head *entry)
{
__list_del(entry->prev, entry->next);
INIT_LIST_HEAD(entry);
}
/**
* list_entry - get the struct for this entry
* @ptr: the &struct list_head pointer.
* @type: the type of the struct this is embedded in.
* @member: the name of the list_struct within the struct.
*/
#define list_entry(ptr, type, member) \
container_of(ptr, type, member)
/**
* list_first_entry - get the first element from a list
* @ptr: the list head to take the element from.
* @type: the type of the struct this is embedded in.
* @member: the name of the list_struct within the struct.
*
* Note, that list is expected to be not empty.
*/
#define list_first_entry(ptr, type, member) \
list_entry((ptr)->next, type, member)
/**
* list_first_entry_or_null - get the first element from a list
* @ptr: the list head to take the element from.
* @type: the type of the struct this is embedded in.
* @member: the name of the list_struct within the struct.
*
* Note that if the list is empty, it returns NULL.
*/
#define list_first_entry_or_null(ptr, type, member) \
(!list_empty(ptr) ? list_first_entry(ptr, type, member) : NULL)
/**
* list_next_entry - get the next element in list
* @pos: the type * to cursor
* @member: the name of the list_struct within the struct.
*/
#define list_next_entry(pos, member) \
list_entry((pos)->member.next, typeof(*(pos)), member)
/**
* list_prev_entry - get the prev element in list
* @pos: the type * to cursor
* @member: the name of the list_struct within the struct.
*/
#define list_prev_entry(pos, member) \
list_entry((pos)->member.prev, typeof(*(pos)), member)
/**
* list_for_each - iterate over a list
* @pos: the &struct list_head to use as a loop cursor.
* @head: the head for your list.
*/
#define list_for_each(pos, head) \
for (pos = (head)->next; pos != (head); pos = pos->next)
/**
* __list_for_each - iterate over a list
* @pos: the &struct list_head to use as a loop cursor.
* @head: the head for your list.
*
* This variant doesn't differ from list_for_each() any more.
* We don't do prefetching in either case.
*/
#define __list_for_each(pos, head) \
for (pos = (head)->next; pos != (head); pos = pos->next)
/**
* list_for_each_prev - iterate over a list backwards
* @pos: the &struct list_head to use as a loop cursor.
* @head: the head for your list.
*/
#define list_for_each_prev(pos, head) \
for (pos = (head)->prev; pos != (head); pos = pos->prev)
/**
* list_for_each_safe - iterate over a list safe against removal of list entry
* @pos: the &struct list_head to use as a loop cursor.
* @n: another &struct list_head to use as temporary storage
* @head: the head for your list.
*/
#define list_for_each_safe(pos, n, head) \
for (pos = (head)->next, n = pos->next; pos != (head); \
pos = n, n = pos->next)
/**
* list_for_each_entry - iterate over list of given type
* @pos: the type * to use as a loop cursor.
* @head: the head for your list.
* @member: the name of the list_struct within the struct.
**/
#define list_for_each_entry(pos, head, member) \
for (pos = list_entry((head)->next, typeof(*pos), member); \
&pos->member != (head); \
pos = list_entry(pos->member.next, typeof(*pos), member))
#endif