Linux Network Programming 5 & mdash; Use UDP protocol for group chat and network programming udp

Source: Internet
Author: User

Linux Network Programming 5-use UDP protocol for group chat and network programming udp
Introduction

The functions implemented in this article are similar to what I wrote earlier (select System Call _ 2 in Linux). The difference is that the communication methods between processes are different. In the previous article, I used pipelines. In this article, I will use the socket interface.

Requirement

The client sends a message to the server. After receiving the message, the server forwards the message to all clients.

Ideas

1. The server maintains a linked list to store the contact information of the client. The structure is as follows:

typedef struct sockaddr_in SA ;typedef struct client_tag{    SA ct_addr;    struct client_tag* ct_next;}CNODE, *pcNODE;

2. The server creates a socket port to receive messages sent by the client. Messages can be classified into: Online notification, offline notification, and chat information. Because the message categories are different, we use struct to encapsulate the messages sent by the client as follows:

#define TYPE_ON   1#define TYPE_OFF  2#define TYPE_CHAT 3
#define SIZE 1024
Typedef struct msg_tag {int msg_type; int msg_len;/* actual message length */char msg_buf [SIZE];} MSG, * pMSG;

Note: The socket port created by the server must be bound to its own contact information so that other clients can send messages (sendto function) to the server.

3. The server uses the select round robin function to listen to its own socket port. When the returned value is 0 (no client sends messages within the polling time) or-1 (receives a signal, an error occurs), the polling continues. When the returned value is 1, a client sends messages. You can obtain the contact information of the client from the outgoing parameters of the recvfrom function. At this time, the contact information is processed based on the received MSG type. If the MSG type is online, the contact information of the client is added to the linked list. If the MSG type is offline, the client is deleted from the linked list. If the MSG type is chat information, then the server forwards it to all clients.

Code

Let's talk about it tomorrow. Big brother needs to watch the movie first.


How to write the tcp protocol in linux network programming?

C/C ++ Basics
Linux basics: laruence's linux Private food
Computer Network
Advanced Programming in UNIX environment
I hope it will be helpful to you.


What is the basic principle of the UDP multicast technology-based chat server and its client in linux?

IGMP

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.