TCP服务器实现将客服端发送的信息广播发送(使用内核链表管理客户端信息)

目录

1.服务器端实现思路

2.服务器端代码

3.客户端代码

4.内核链表代码

5.运行格式

一、服务器端

二、客户端

6.效果


1.服务器端实现思路

  1. Tcp广播服务初始化

  2. 等待客户端连接

  3. 广播发送

2.服务器端代码

#include "list.h"
#include <signal.h>
#define EXIT_MASK "exit"pthread_mutex_t mutex;
volatile int is_down = 0;void *Tcp_Pthreads_Broadcast(void *arg)
{service_inf_poi sip = (service_inf_poi)arg;// 设置线程分离if (pthread_detach(pthread_self()) != 0){perror("pthread_detach error");close(sip->ser_fd);pthread_exit((void *)(-1));}char msg[MSG_MAX_LEN] = "\0";while (!is_down){memset(msg, 0, sizeof(char) * MSG_MAX_LEN);// 保存当前已经连接的客户端的IP地址和套接字int cur_client_id = sip->cur_client_node->client_own_id;char cur_client_ip_addr[IP_ADDR_LEN] = "\0";strcpy(cur_client_ip_addr, sip->cur_client_node->client_ip_addr);// 根据套接字读取数据int read_ret = read(cur_client_id, msg, MSG_MAX_LEN);if (read_ret == -1){perror("read error...");close(sip->ser_fd);pthread_exit((void *)(-1));}else if (read_ret == 0 || strcmp(msg, EXIT_MASK) == 0){printf("%s 断开连接\n", cur_client_ip_addr);client_link pos = NULL;// 删除该客户端节点,并结束该进程list_for_each_entry(pos, &sip->client_list_head->little_pointer_head, little_pointer_head){if (pos->client_own_id == cur_client_id) // 根据套接字 号码来找{break;}}pthread_mutex_lock(&mutex); // 上锁list_del(&pos->little_pointer_head);pthread_mutex_unlock(&mutex); // 解锁printf("删除节点成功\n\n");// 判断当前是否有客户if (list_empty(&sip->client_list_head->little_pointer_head) == 1 || sip->client_list_head == NULL || &sip->client_list_head->little_pointer_head == NULL){printf("================当前无客户连接======================\n\n");printf("服务器端即将断开!!!\n\n");// 退出,并释放,结束服务器端pthread_mutex_lock(&mutex); // 上锁Tcp_Server_Broadcast_Free(sip);is_down = 1;close(sip->ser_fd);pthread_mutex_unlock(&mutex); // 解锁if (kill(getpid(), SIGKILL) == -1){perror("kill error...");pthread_exit((void *)-1);}break;}else{pos = NULL;printf("=============当前客户端列表==========================\n");list_for_each_entry(pos, &sip->client_list_head->little_pointer_head, little_pointer_head){printf("%s\n", pos->client_ip_addr);}printf("===================================================\n\n");}break; // 结束当前线程}else{printf("%s : %s\n", cur_client_ip_addr, msg);// 广播转发client_link pos = NULL;// 将前16个字节作为ip地址char new_msg[MSG_MAX_LEN] = "\0";sprintf(new_msg, "%s:【%s】", cur_client_ip_addr, msg);printf("new_msg = %s\n", new_msg);list_for_each_entry(pos, &sip->client_list_head->little_pointer_head, little_pointer_head){if (strcmp(cur_client_ip_addr, pos->client_ip_addr) != 0) // 自己不转发给自己{if (write(pos->client_own_id, new_msg, strlen(new_msg)) == -1){perror("write error...");break;}printf("转发给:%s成功!\n", pos->client_ip_addr);}}printf("\n");}}pthread_exit((void *)0);return NULL;
}void Tcp_Server_Broadcast_Free(service_inf_poi sip)
{free(sip);return;
}
// 创建新节点
client_link Create_New_Client_Node()
{client_link new_client_node = (client_link)malloc(sizeof(client_node));if (new_client_node == (client_link)NULL){perror("malloc new_big_node error");return (client_link)-1;}memset(new_client_node, 0, sizeof(client_node));INIT_LIST_HEAD(&new_client_node->little_pointer_head);return new_client_node;
}// Tcp广播服务初始化
service_inf_poi Tcp_Server_Broadcast_Init(int ser_port)
{service_inf_poi sip = (service_inf_poi)malloc(sizeof(service_inf));if (sip == (service_inf_poi)NULL){perror("malloc error...");return (service_inf_poi)-1;}memset(sip, 0, sizeof(service_inf));if ((sip->ser_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1){perror("socket error...");return (service_inf_poi)-1;}// 创建客户端头结点sip->client_list_head = Create_New_Client_Node();if (sip->client_list_head == (client_link)-1){return (service_inf_poi)-1;}// 设置基本信息struct sockaddr_in ser_inf;memset(&ser_inf, 0, sizeof(ser_inf));ser_inf.sin_family = AF_INET;ser_inf.sin_port = htons(ser_port); // 将小端变成大端ser_inf.sin_addr.s_addr = htonl(INADDR_ANY);// 绑定if (bind(sip->ser_fd, (struct sockaddr *)&ser_inf, sizeof(ser_inf)) == -1){perror("bind error...");return (service_inf_poi)-1;}// 监听if (listen(sip->ser_fd, CLIENT_MAX_CONNECT_NUM / 4) == -1) // 最大等待队列是CLIENT_MAX_CONNECT_NUM / 4个{perror("listen error...");return (service_inf_poi)-1;}// 初始化互斥锁if (pthread_mutex_init(&mutex, NULL)){perror("pthread_mutex error...\n");return (service_inf_poi)-1;}return sip;
}// 等待客户端连接
int Waiting_For_Connnect(service_inf_poi sip)
{struct sockaddr_in client_inf;int len = sizeof(client_inf);while (1){memset(&client_inf, 0, len);int new_client_fd = accept(sip->ser_fd, (struct sockaddr *)&client_inf, &len);if (new_client_fd == -1){perror("accept error...");return -1;}printf("%s已经连接服务器\n", inet_ntoa(client_inf.sin_addr));// 创建新节点client_link new_client_node = Create_New_Client_Node();if (new_client_node == (client_link)-1){return -1;}// 将ip和新的套接字 赋值new_client_node->client_own_id = new_client_fd;strcpy(new_client_node->client_ip_addr, inet_ntoa(client_inf.sin_addr));sip->cur_client_node = new_client_node; // 保存当前的结点// 将新节点插入到客户端列表中list_add_tail(&new_client_node->little_pointer_head, &sip->client_list_head->little_pointer_head);printf("添加头结点成功!\n\n");printf("======================当前客户端列表=======================\n");client_link pos;list_for_each_entry(pos, &sip->client_list_head->little_pointer_head, little_pointer_head){printf("%s\n", pos->client_ip_addr);}printf("==========================================================\n\n");// 创建线程进行广播发送pthread_t pid;if (pthread_create(&pid, NULL, Tcp_Pthreads_Broadcast, sip) != 0){perror("pthread_create error...");return -1;}}return 0;
}int main(int argc, char *argv[])
{if (argc != 2)return -1;service_inf_poi sip = Tcp_Server_Broadcast_Init(atoi(argv[1]));if (sip == (service_inf_poi)-1){printf("Tcp服务器初始化失败!\n");return -1;}else{printf("Tcp服务器初始化成功!正在等待接受数据.......\n");}Waiting_For_Connnect(sip);return 0;
}

3.客户端代码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <pthread.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <signal.h>#define IP_ADDR_LEN 16
#define MSG_MAX_LEN 256
#define CLIENT_MAX_CONNECT_NUM 100
#define EXIT_MASK "exit"volatile int is_over = 0;int Client_Init(char *server_ip_addr, int server_prot_num);
int Client_Running(int cli_fd);
void *Send_Msg(void *arg);
void *Rec_Msg(void *arg);void *Send_Msg(void *arg)
{int *client_fd = (int *)(arg);int cli_fd = *(client_fd);printf("Send_msg = %d\n", cli_fd);if (pthread_detach(pthread_self()) != 0){perror("pthread_detach error");close(cli_fd);free(client_fd);pthread_exit((void *)(-1));}char msg[MSG_MAX_LEN] = "\0";while (!is_over){memset(msg, 0, MSG_MAX_LEN);printf("请输入要发送的数据:");scanf("%s", msg);if (write(cli_fd, msg, strlen(msg)) == -1){perror("Send_Msg:write error...");close(cli_fd);free(client_fd);pthread_exit((void *)-1);}if (strcmp(EXIT_MASK, msg) == 0){printf("我要断了\n");is_over = 1;if (kill(getpid(), SIGKILL) == -1){perror("kill error...");close(cli_fd);free(client_fd);pthread_exit((void *)-1);}break;}}close(cli_fd);free(client_fd);pthread_exit((void *)0);return NULL;
}
void *Rec_Msg(void *arg)
{int cli_fd = *((int *)arg);if (pthread_detach(pthread_self()) != 0){perror("pthread_detach error");close(cli_fd);pthread_exit((void *)(-1));}char msg[MSG_MAX_LEN] = "\0";while (!is_over){memset(msg, 0, MSG_MAX_LEN);int read_ret = read(cli_fd, msg, MSG_MAX_LEN);if (read_ret == -1){perror("write error...");close(cli_fd);pthread_exit((void *)-1);}else if (read_ret != 0){printf("\n%s\n", msg);}}close(cli_fd);pthread_exit((void *)0);return NULL;
}int Client_Init(char *server_ip_addr, int server_prot_num)
{// 创建套接字int cli_fd = socket(AF_INET, SOCK_STREAM, 0);if (cli_fd == -1){perror("socket error...");return -1;}else{printf("socket success %d\n", cli_fd);}struct sockaddr_in cli_inf;memset(&cli_inf, 0, sizeof(cli_inf));cli_inf.sin_family = AF_INET;cli_inf.sin_addr.s_addr = inet_addr(server_ip_addr);cli_inf.sin_port = htons(server_prot_num);// 连接if (connect(cli_fd, (struct sockaddr *)&cli_inf, sizeof(cli_inf)) == -1){perror("connect error...");close(cli_fd);return -1;}else{printf("连接成功!\n");}return cli_fd;
}int Client_Running(int cli_fd)
{int *client_fd = (int *)malloc(sizeof(int));*client_fd = cli_fd;pthread_t pid_send, pid_rec;if (pthread_create(&pid_send, NULL, Send_Msg, client_fd) != 0){perror("pthread_create error...");return -1;}if (pthread_create(&pid_rec, NULL, Rec_Msg, client_fd) != 0){perror("pthread_create error...");return -1;}pause();return 0;
}// a.out ip port
int main(int argc, char *argv[])
{if (argc != 3){printf("输入的参数不对!\n");return -1;}int cli_fd = Client_Init(argv[1], atoi(argv[2]));printf("Client_Init success %d\n", cli_fd);if (cli_fd == -1){printf("Client Init error\n");return -1;}if (Client_Running(cli_fd) == -1){printf("Client_Running error\n");return -1;}return 0;
}

4.内核链表代码

#ifndef _LINUX_LIST_H
#define _LINUX_LIST_H#include <stdio.h>
#include <stdbool.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <pthread.h>
#include <arpa/inet.h>
#include <netinet/in.h>#define IP_ADDR_LEN 16
#define MSG_MAX_LEN 256
#define CLIENT_MAX_CONNECT_NUM 100/** 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)struct list_head
{struct list_head *next, *prev;
};typedef struct big_list_node
{int client_own_id;				  // 客户端的套接字char client_ip_addr[IP_ADDR_LEN]; // 客户端的ip地址struct list_head little_pointer_head;
} client_node, *client_link;typedef struct tcp_service_inf
{int ser_fd;					  // 服务端的套接字client_link cur_client_node;  // 存放当前客户端的结点client_link client_list_head; // 存放客户端链表的头结点
} service_inf, *service_inf_poi;client_link Create_New_Client_Node();
service_inf_poi Tcp_Server_Broadcast_Init(int ser_port);
client_link Create_Client_Node();
int Waiting_For_Connnect(service_inf_poi sip);
void Tcp_Server_Broadcast_Free(service_inf_poi sip);
void *Tcp_Pthreads_Broadcast(void *arg);static inline void INIT_LIST_HEAD(struct list_head *list)
{list->next = list; // 游离节点指向小头list->prev = list;
}#ifdef CONFIG_DEBUG_LIST
extern bool __list_add_valid(struct list_head *new,struct list_head *prev,struct list_head *next);
extern bool __list_del_entry_valid(struct list_head *entry);
#else
static inline bool __list_add_valid(struct list_head *new,struct list_head *prev,struct list_head *next)
{return true;
}
static inline bool __list_del_entry_valid(struct list_head *entry)
{return true;
}
#endif/** Insert a new entry between two known consecutive entries.** This is only for internal list manipulation where we know* the prev/next entries already!*/
static inline void __list_add(struct list_head *new,struct list_head *prev,struct list_head *next)
{if (!__list_add_valid(new, prev, next))return;next->prev = new;new->next = next;new->prev = prev;prev->next = new;
}/*** 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.*/
static inline void __list_del_entry(struct list_head *entry)
{if (!__list_del_entry_valid(entry))return;__list_del(entry->prev, entry->next);
}static inline void list_del(struct list_head *entry)
{__list_del_entry(entry);entry->next = NULL;entry->prev = NULL;
}/*** 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(entry);INIT_LIST_HEAD(entry);
}/*** list_move - delete from one list and add as another's head* @list: the entry to move* @head: the head that will precede our entry*/
static inline void list_move(struct list_head *list, struct list_head *head)
{__list_del_entry(list);list_add(list, head);
}/*** list_move_tail - delete from one list and add as another's tail* @list: the entry to move* @head: the head that will follow our entry*/
static inline void list_move_tail(struct list_head *list,struct list_head *head)
{__list_del_entry(list);list_add_tail(list, head);
}/*** list_is_last - tests whether @list is the last entry in list @head* @list: the entry to test* @head: the head of the list*/
static inline int list_is_last(const struct list_head *list,const struct list_head *head)
{return list->next == head;
}/*** list_empty - tests whether a list is empty* @head: the list to test.*/
static inline int list_empty(const struct list_head *head)
{return head->next == head;
}/*** list_empty_careful - tests whether a list is empty and not being modified* @head: the list to test** Description:* tests whether a list is empty _and_ checks that no other CPU might be* in the process of modifying either member (next or prev)** NOTE: using list_empty_careful() without synchronization* can only be safe if the only activity that can happen* to the list entry is list_del_init(). Eg. it cannot be used* if another CPU could re-list_add() it.*/
static inline int list_empty_careful(const struct list_head *head)
{struct list_head *next = head->next;return (next == head) && (next == head->prev);
}/*** list_rotate_left - rotate the list to the left* @head: the head of the list*/
static inline void list_rotate_left(struct list_head *head)
{struct list_head *first;if (!list_empty(head)){first = head->next;list_move_tail(first, head);}
}/*** list_is_singular - tests whether a list has just one entry.* @head: the list to test.*/
static inline int list_is_singular(const struct list_head *head)
{return !list_empty(head) && (head->next == head->prev);
}static inline void __list_cut_position(struct list_head *list,struct list_head *head, struct list_head *entry)
{struct list_head *new_first = entry->next;list->next = head->next;list->next->prev = list;list->prev = entry;entry->next = list;head->next = new_first;new_first->prev = head;
}/*** list_cut_position - cut a list into two* @list: a new list to add all removed entries* @head: a list with entries* @entry: an entry within head, could be the head itself*	and if so we won't cut the list** This helper moves the initial part of @head, up to and* including @entry, from @head to @list. You should* pass on @entry an element you know is on @head. @list* should be an empty list or a list you do not care about* losing its data.**/
static inline void list_cut_position(struct list_head *list,struct list_head *head, struct list_head *entry)
{if (list_empty(head))return;if (list_is_singular(head) &&(head->next != entry && head != entry))return;if (entry == head)INIT_LIST_HEAD(list);else__list_cut_position(list, head, entry);
}static inline void __list_splice(const struct list_head *list,struct list_head *prev,struct list_head *next)
{struct list_head *first = list->next;struct list_head *last = list->prev;first->prev = prev;prev->next = first;last->next = next;next->prev = last;
}/*** list_splice - join two lists, this is designed for stacks* @list: the new list to add.* @head: the place to add it in the first list.*/
static inline void list_splice(const struct list_head *list,struct list_head *head)
{if (!list_empty(list))__list_splice(list, head, head->next);
}/*** list_splice_tail - join two lists, each list being a queue* @list: the new list to add.* @head: the place to add it in the first list.*/
static inline void list_splice_tail(struct list_head *list,struct list_head *head)
{if (!list_empty(list))__list_splice(list, head->prev, head);
}/*** list_splice_init - join two lists and reinitialise the emptied list.* @list: the new list to add.* @head: the place to add it in the first list.** The list at @list is reinitialised*/
static inline void list_splice_init(struct list_head *list,struct list_head *head)
{if (!list_empty(list)){__list_splice(list, head, head->next);INIT_LIST_HEAD(list);}
}/*** list_splice_tail_init - join two lists and reinitialise the emptied list* @list: the new list to add.* @head: the place to add it in the first list.** Each of the lists is a queue.* The list at @list is reinitialised*/
static inline void list_splice_tail_init(struct list_head *list,struct list_head *head)
{if (!list_empty(list)){__list_splice(list, head->prev, head);INIT_LIST_HEAD(list);}
}// 在stddef.h中
#define offsetof(TYPE, MEMBER) ((size_t) & ((TYPE *)0)->MEMBER)
// 在kernel.h中
#define container_of(ptr, type, member) ({                      \const typeof( ((type *)0)->member ) *__mptr = (ptr);    \(type *)( (char *)__mptr - offsetof(type,member) ); })/*** 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_head 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_head 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_last_entry - get the last 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_head within the struct.** Note, that list is expected to be not empty.*/
#define list_last_entry(ptr, type, member) \list_entry((ptr)->prev, 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_head within the struct.** Note that if the list is empty, it returns NULL.*/
#define list_first_entry_or_null(ptr, type, member) ({        \struct list_head *head__ = (ptr);                         \struct list_head *pos__ = head__->next;                   \pos__ != head__ ? list_entry(pos__, type, member) : NULL; \
})/*** list_next_entry - get the next element in list* @pos:	the type * to cursor* @member:	the name of the list_head 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_head 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_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_prev_safe - iterate over a list backwards 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_prev_safe(pos, n, head) \for (pos = (head)->prev, n = pos->prev;   \pos != (head);                       \pos = n, n = pos->prev)/*** 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_head within the struct.*/
#define list_for_each_entry(pos, head, member)               \for (pos = list_first_entry(head, typeof(*pos), member); \&pos->member != (head);                             \pos = list_next_entry(pos, member))/*** list_for_each_entry_reverse - iterate backwards 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_head within the struct.*/
#define list_for_each_entry_reverse(pos, head, member)      \for (pos = list_last_entry(head, typeof(*pos), member); \&pos->member != (head);                            \pos = list_prev_entry(pos, member))/*** list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue()* @pos:	the type * to use as a start point* @head:	the head of the list* @member:	the name of the list_head within the struct.** Prepares a pos entry for use as a start point in list_for_each_entry_continue().*/
#define list_prepare_entry(pos, head, member) \((pos) ?: list_entry(head, typeof(*pos), member))/*** list_for_each_entry_continue - continue iteration 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_head within the struct.** Continue to iterate over list of given type, continuing after* the current position.*/
#define list_for_each_entry_continue(pos, head, member) \for (pos = list_next_entry(pos, member);            \&pos->member != (head);                        \pos = list_next_entry(pos, member))/*** list_for_each_entry_continue_reverse - iterate backwards from the given point* @pos:	the type * to use as a loop cursor.* @head:	the head for your list.* @member:	the name of the list_head within the struct.** Start to iterate over list of given type backwards, continuing after* the current position.*/
#define list_for_each_entry_continue_reverse(pos, head, member) \for (pos = list_prev_entry(pos, member);                    \&pos->member != (head);                                \pos = list_prev_entry(pos, member))/*** list_for_each_entry_from - iterate over list of given type from the current point* @pos:	the type * to use as a loop cursor.* @head:	the head for your list.* @member:	the name of the list_head within the struct.** Iterate over list of given type, continuing from current position.*/
#define list_for_each_entry_from(pos, head, member) \for (; &pos->member != (head);                  \pos = list_next_entry(pos, member))/*** list_for_each_entry_safe - iterate over list of given type safe against removal of list entry* @pos:	the type * to use as a loop cursor.* @n:		another type * to use as temporary storage* @head:	the head for your list.* @member:	the name of the list_head within the struct.*/
#define list_for_each_entry_safe(pos, n, head, member)       \for (pos = list_first_entry(head, typeof(*pos), member), \n = list_next_entry(pos, member);                    \&pos->member != (head);                             \pos = n, n = list_next_entry(n, member))/*** list_for_each_entry_safe_continue - continue list iteration safe against removal* @pos:	the type * to use as a loop cursor.* @n:		another type * to use as temporary storage* @head:	the head for your list.* @member:	the name of the list_head within the struct.** Iterate over list of given type, continuing after current point,* safe against removal of list entry.*/
#define list_for_each_entry_safe_continue(pos, n, head, member) \for (pos = list_next_entry(pos, member),                    \n = list_next_entry(pos, member);                       \&pos->member != (head);                                \pos = n, n = list_next_entry(n, member))/*** list_for_each_entry_safe_from - iterate over list from current point safe against removal* @pos:	the type * to use as a loop cursor.* @n:		another type * to use as temporary storage* @head:	the head for your list.* @member:	the name of the list_head within the struct.** Iterate over list of given type from current point, safe against* removal of list entry.*/
#define list_for_each_entry_safe_from(pos, n, head, member) \for (n = list_next_entry(pos, member);                  \&pos->member != (head);                            \pos = n, n = list_next_entry(n, member))/*** list_for_each_entry_safe_reverse - iterate backwards over list safe against removal* @pos:	the type * to use as a loop cursor.* @n:		another type * to use as temporary storage* @head:	the head for your list.* @member:	the name of the list_head within the struct.** Iterate backwards over list of given type, safe against removal* of list entry.*/
#define list_for_each_entry_safe_reverse(pos, n, head, member) \for (pos = list_last_entry(head, typeof(*pos), member),    \n = list_prev_entry(pos, member);                      \&pos->member != (head);                               \pos = n, n = list_prev_entry(n, member))/*** list_safe_reset_next - reset a stale list_for_each_entry_safe loop* @pos:	the loop cursor used in the list_for_each_entry_safe loop* @n:		temporary storage used in list_for_each_entry_safe* @member:	the name of the list_head within the struct.** list_safe_reset_next is not safe to use in general if the list may be* modified concurrently (eg. the lock is dropped in the loop body). An* exception to this is if the cursor element (pos) is pinned in the list,* and list_safe_reset_next is called after re-taking the lock and before* completing the current iteration of the loop body.*/
#define list_safe_reset_next(pos, n, member) \n = list_next_entry(pos, member)/** Double linked lists with a single pointer list head.* Mostly useful for hash tables where the two pointer list head is* too wasteful.* You lose the ability to access the tail in O(1).*/#endif

5.运行格式

一、服务器端

gcc xx.c -pthrad -o s

./s 8888

其中8888是端口号

二、客户端

gcc xxx.c -pthrad -o c

./s 192.xxx.xxx.xxx 8888

第二个参数是:服务器端的ip地址

第三个参数是:端口号

(注意:如果是同一台主机,则端口号不能相同)

6.效果

连接效果

断开效果

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.rhkb.cn/news/328957.html

如若内容造成侵权/违法违规/事实不符,请联系长河编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

如何看固态硬盘是否支持trim功能?固态硬盘开启trim数据还能恢复吗

随着科技的飞速发展&#xff0c;固态硬盘&#xff08;SSD&#xff09;已成为电脑存储的主流选择。相较于传统的机械硬盘&#xff0c;固态硬盘以其高速读写和优秀的耐用性赢得了广泛好评。而在固态硬盘的众多功能中&#xff0c;TRIM功能尤为关键&#xff0c;它能有效提升固态硬盘…

上传文件,服务器报500错误

项目场景&#xff1a; 今天项目上出现一个耗时比较长的问题&#xff0c;但是问题很简单&#xff0c;一开始没注意&#xff0c;导致耗时很久&#xff0c;到底是咋回事儿呢&#xff0c;请看下文~~ 问题描述 用户使用APP上传图片&#xff0c;出现 附件上传失败:服务器响应失败 的…

The 13th Shandong ICPC Provincial Collegiate Programming Contest

The 13th Shandong ICPC Provincial Collegiate Programming Contest The 13th Shandong ICPC Provincial Collegiate Programming Contest A. Orders 题意&#xff1a;有n个订单&#xff0c; 每日可生产k个产品&#xff0c;每个订单给出交付日和交付数量&#xff0c;是否能…

究极完整版!!Centos6.9安装最适配的python和yum,附带教大家如何写Centos6.9的yum.repos.d配置文件。亲测可行!

前言&#xff01; 这里我真是要被Centos6.9给坑惨了&#xff0c;最刚开始学习linux的时候并没有在意那么的&#xff0c;没有考虑到选版本问题&#xff0c;直到23年下半年&#xff0c;官方不维护Centos6.9了&#xff0c;基本上当时配置的文件和安装的依赖都用不了了&#xff0c…

OpenAI发布会最新消息!ChatGPT新功能发布!

关于即将发布的内容&#xff0c;OpenAI 官方帖子提供的唯一细节是&#xff0c;此次发布将更新 ChatGPT 及其最新模型 GPT-4。 OpenAI 员工程博文&#xff08;Bowen Cheng&#xff09;跟了个帖&#xff0c;「比 gpt-5 更酷」&#xff0c;不过又迅速删帖。 OpenAI 的葫芦里到底卖…

电商秒杀系统设计

业务流程 系统架构 系统挑战 高并发:秒杀活动会在短时间内吸引大量用户,系统需要能够处理高峰时期的大量并发请求 库存同步:在秒杀中,面临的一个严重系统挑战是如何确保在数以万计的用户同时抢购有限的商品时,如何正确、实时地扣减库存,以防止超卖现象。 防止恶意抢购和…

儿童护眼台灯哪个牌子好,适合儿童使用的护眼台灯推荐

护眼台灯在近几年成为家长和经常与电子设备打交道的人士中备受瞩目的家用电器。对于有孩子的家庭而言&#xff0c;它几乎成为了必备品&#xff0c;许多消费者已经对其有了一定的了解并进行了购买。然而&#xff0c;仍有部分家长对护眼台灯的效果和重要性缺乏充分认识&#xff0…

Dreamweaver 2021 for Mac 激活版:网页设计工具

在追求卓越的网页设计道路上&#xff0c;Dreamweaver 2021 for Mac无疑是您的梦幻之选。这款专为Mac用户打造的网页设计工具&#xff0c;集强大的功能与出色的用户体验于一身。 Dreamweaver 2021支持多种网页标准和技术&#xff0c;让您能够轻松创建符合现代网页设计的作品。其…

iisnginx环境一次奇怪的跨域问题解决经过

跨域问题描述&#xff1a; iis网站跨域、nginx 网站跨域 都已配置&#xff0c;访问接口依然出现跨域问题。 错误提示&#xff1a; ccess to XMLHttpRequest at ‘https://xxx.com/gameapi/preserve/get/status’ from origin ‘https://cdn.xxx.com’ has been blocked by CO…

技术前沿 |【大模型LLaMA:技术原理、优势特点及应用前景探讨】

大模型LLaMA&#xff1a;技术原理、优势特点及应用前景探讨 一、引言二、大模型LLaMA的基本介绍三、大模型LLaMA的优势特点五、结论与展望 一、引言 随着人工智能技术的飞速发展&#xff0c;大模型已成为推动这一领域进步的重要力量。近年来&#xff0c;大模型LLaMA以其卓越的…

每日一题——力扣206. 反转链表(举一反三、思想解读)

一个认为一切根源都是“自己不够强”的INTJ 个人主页&#xff1a;用哲学编程-CSDN博客专栏&#xff1a;每日一题——举一反三题目链接 目录 菜鸡写法​编辑 代码点评 代码分析 时间复杂度 空间复杂度 专业点评 另一种方法​编辑 代码点评 代码逻辑 时间复杂度 空间…

基于火山引擎云搜索的混合搜索实战

在搜索应用中&#xff0c;传统的 Keyword Search 一直是主要的搜索方法&#xff0c;它适合精确匹配查询的场景&#xff0c;能够提供低延迟和良好的结果可解释性&#xff0c;但是 Keyword Search 并没有考虑上下文信息&#xff0c;可能产生不相关的结果。最近几年&#xff0c;基…

第三十二天 | 46.全排列 47.全排列||

终于进入排列&#xff01;&#xff08;之前都是组合&#xff09; 排列和组合的区别&#xff1a;在数学上的区别都懂&#xff0c;主要是看在代码实现上有什么区别 题目&#xff1a;46.全排列 树型结构比较简单 用used标记某一元素是否使用过。在组合问题中&#xff0c;其实是…

【Amplify_自己写的shadr遇到没有阴影的解决方案】

Amplify 自己写的shadr遇到没有阴影的解决方案 2020-01-21 16:04 本来我有个百试很灵的投射阴影脚本。 这次不灵光了&#xff01;地形内建材质&#xff0c;这个不支持投射的阴影~~奇了怪了。 可以采用引用的方式UsePass加入阴影部分代码&#xff0c;具体操作如下&#xff1…

.NET 4.8和.NET 8.0的区别和联系、以及查看本地计算机的.NET版本

文章目录 .NET 4.8和.NET 8.0的区别查看本地计算机的.NET版本 .NET 4.8和.NET 8.0的区别 .NET 8.0 和 .NET 4.8 之间的区别主要体现在它们的发展背景、目标平台、架构设计和功能特性上。下面是它们之间的一些主要区别&#xff1a; 发展背景&#xff1a; .NET 4.8 是.NET Fram…

WSL2-Ubuntu(深度学习环境搭建)

1.在Windows的WSL2上安装Ubuntu 流程可参考&#xff1a;https://www.bilibili.com/video/BV1mX4y177dJ 注意&#xff1a;中间可能需要使用命令wsl --update更新一下wsl。 2.WSL数据迁移 按照下面流程&#xff1a;开始菜单->设置->应用->安装的应用->搜索“ubun…

LAMP与动静态网站介绍

黄金架构LAMP Httpd PHP MySQL 三这如何工作 为什么说LAMP是黄金架构呢&#xff1f; 在互联网刚刚新起时&#xff0c;很多门户网站第一代架构都是采用LAMP&#xff0c;比如新浪&#xff0c;一些电商的互联网公司的站点&#xff0c;他们在网站第一代架构出行&#xff0c;用的都…

JETBRAINS IDES 分享一个2099通用试用码!DataGrip 2024 版 ,支持一键升级

文章目录 废话不多说上教程&#xff1a;&#xff08;动画教程 图文教程&#xff09;一、动画教程激活 与 升级&#xff08;至最新版本&#xff09; 二、图文教程 &#xff08;推荐&#xff09;Stage 1.下载安装 toolbox-app&#xff08;全家桶管理工具&#xff09;Stage 2 : 下…

VUE 滚动到指定区域scrollIntoView

背景&#xff1a;当前 VUE 页面数据量很大&#xff0c;右侧出现滚动条, 进入该页面&#xff0c;页面定位到指定区域&#xff1b; 项目要求&#xff1a; 进入页面&#xff0c;定位到指定行&#xff08;红色标记&#xff09; 直接看效果&#xff1a; 代码demo&#xff1a; <…

Unity设计模式之工厂模式

什么是工厂模式&#xff1f; 工厂是一种创建型设计模式。通俗来讲就是提供一种封装对象创建的方式&#xff0c;将对象的创建和使用区分开。就是Unity里面通常用到的创建和管理对象。 工厂模式有什么优点&#xff1f; 1、封装对象的创建方式&#xff0c;使其更加灵活、易于管理…