Socket Communication UDP file transmission (multiple clients)-source code

Source: Internet
Author: User
Tags format definition

Program requirements:

  1. Programs are divided into server programs and client programs. The client initiates a request for a specified file and transmits the file to the client.
  2. One server supports communication between multiple clients.
  3. Data packets must have a verification mechanism.
  4. There is a packet loss retransmission mechanism.
  5. Supports resumable data transfer.
  6. Can handle various exceptions normally.

Implementation Process

Server

1. Develop UDP Communication Protocol

Design a UDP Header

Type
Len
ID
Check
Data

1. Type (data type): Char

The data type of the UDP packet and the server processing method are provided. I divide it into several different values: File Transfer Application, confirmation after the client receives a packet (re-transmission, continue)

Resumable upload (after disconnection, re-establish the connection and perform the previous transmission), end the file transfer (from the server to the client), and cause File Transfer errors.

2. Len (packet length): int

3. Check (checksum): int

Perform CRC checksum on the package to prevent errors during data transmission.

4. ID (ID): Long int

Label the data packet during file transmission, and mark the position of the file breakpoint during resumable upload (changed according to the type requirement)


Ii. Server event handling mechanism

At first, I plan to use a linked list to receive and store client requests, use a linked list queue to process the corresponding requests (another processing thread), and a timeout processing queue. The final findings become very complex (not very skilled in linked list operations ). We decided to replace the above three linked lists with only one global array.

Start two threads. One thread is used to receive data sent from the client cyclically and mark the data into the array. The other thread is used to process the events to be processed in the array. (One client and one array element)

Each client allocates a struct and puts it in an array.

Customer struct: Flag, clinet_addr, timer, struct file_info, etc.

Flag: Mark the client status (waiting for transmission, server waiting for reflection, timeout, empty client, etc)

Timer: record the time point finally reflected by the client, used to determine the timeout.

File_info: (file path, file pointer, file offset) records file transmission.


The main thread cyclically determines whether a client connection times out.


Client

Apply for a file from the server (first determine whether the breakpoint is enabled). When the breakpoint is re-transmitted, use the fseek function to locate the breakpoint and send the breakpoint information to the server. Then, the server re-reads the file and starts transmission. After the request is sent, wait for the server to reply. After the server replies, it starts to count and accept the packages. The server sends a serial number to each package. Each time the client receives a UDP packet, it puts it into the buffer zone. When the buffer zone is slow, it writes the serial number to the file in sequence. When there is no number to be written, send the ACK to the server.


Header file

# Include <sys/socket. h>
# Include <string. h>
# Include <netinet/in. h>
# Include <stdio. h>
# Include <fcntl. h>
# Include <time. h>
# Include <strings. h>
# Include <sys/STAT. h>
# Include <stdlib. h>
# Include <unistd. h>
# Include <sys/select. h>
# Include <sys/time. h>
# Include <sys/types. h>

/* Udp_datapack-> type */
# Define req_file 0
# Define ack_true 1
# Define ack_fail 2
# Define ack_reconnect 3
# Define file_date 4

/* Udp_req-> flag UDP type */
# Define clinet_empty 0
# Define clinet_first_req 1
# Define clinet_time_out 2
# Define clinet_link_ack_wait 3
# Define clinet_link_ack_true 4
# Define clinet_link_ack_fail 5

/* Send_ack flag. The label value in the UDP header when UDP is ack */
/* Udp_datapack-> type = ack_true | ack_fail ==> udp_datapack-> label */
# Define server_clinet_full 0
# Define find_file 1
# Define unfind_file 2
# Define file_over 3
# Define type_unrecognized 4
# Define crc_fail 5
# Define file_read_fail 6

/* Clinet file flag (file_pack_num )*/
# Define file_complete-1
# Define file_incomplete-2
# Define server_busy-3
# Define file_not_exist-4
# Define file_serv_read_fail-5
# Define re_apply-6

# Define serv_port 8888/* Port Number */
# Define Max 1024/* UDP data part size */
# Define max_udp_clinet 10/* the server handles the largest number of clients */

# Define udp_head_len 13/* UDP header size */

# Define timeval 45/* timeout */

/* UDP packet format definition */
Struct udp_datapack {
Char type;
/* Data packet type */
Long int label;
/* File Package number, request package file location information */
Int size;
/* Package size */
Int check;
/* CRC */
Char data [Max];
};

/* Transmitted file information */
Struct file_infor {
Char * file_path;
File * file_fp;
Off_t seek_flag;
};

/* Client information saved by the server */
Struct udp_req {
Char flag;
/* Client status */
Int fsend_label;
/* Change with flag */
Struct sockaddr_in req_addr;
/* Client address */
Struct file_infor req_file;
/* File information requested by the customer */
Struct timeval timeout;
/* Time-out */
};

/* Local socket information */
Struct local_infor {
Int sockfd;
Struct sockaddr_in local_addr;
};


Complete source code: http://download.csdn.net/user/jmq_0000

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.