Multi-threaded TCP server project [Open Source], multi-threaded tcp Open Source

Source: Internet
Author: User

Multi-threaded TCP server project [Open Source], multi-threaded tcp Open Source

This document provides a complete TCP Server instance, including dynamic Connection Library, unit test, acceptance test, and Winform simulation test. For beginners to learn, I hope the veteran will give more comments.

Project address: https://tcpserversocket.codeplex.com/(project can be directly downloaded)

System Structure

The project file is as follows:


TcpServerSocket: the core dynamic link library of the project. If it is used in another project, you only need to reference the DLL generated by the project;

WindowsFormsApplication1: A simple winform application that allows you to quickly learn how to use it in WinForm;

UnitTestProject: unit test;

AcceptanceTest: Acceptance Test, simulating a client with a high-speed connection disconnection to send data quickly.

 

The core of the project is to use TcpListener for listening and NetworkStream for reading and writing TCP data streams. The core principles of the server are as follows:

 


System Test

WindowsFormsApplication1 + network debugging assistant test result:


WindowsFormsApplication1 + AcceptanceTest:


 

How to Use

You can add the generated TcpServerSocket. dll in your project, and then start the TCP Server journey with a few simple codes!

 var tcp = new TcpServer
 {
     RecvNewClientAction = handler => Debug.WriteLine("recv new client: " + handler),
     LostClientAction = handler => Debug.WriteLine("lost client :" + handler),
      RecvDataAction = (ip, data, len) => Debug.WriteLine("{0}:{1}", ip, Encoding.ASCII.GetString(data, 0, len)),
     ListenPort = 8080
 };
 tcp.StartListen();

Subsequent Development

One major problem of the program is to handle abnormal disconnection situations such as network disconnection and client program crash. Originally, we wanted to add heartbeat processing in the program, but this increased the complexity of the program. I hope you can give me some advice!


Design a linux C language, TCP-based multi-thread server and client sending Process

Client
# Include <stdio. h>

# Include <unistd. h>

# Include <strings. h>

# Include <sys/types. h>

# Include <sys/socket. h>

# Include <netinet/in. h>

# Include <netdb. h>

# Define PORT 1234

# Define maxdatasalize 1000

Void process (FILE * fp, int sockfd );

Char * getMessage (char * sendline, int len, FILE * fp );

Int main (int argc, char * argv [])

{

Int fd;

Struct hostent * he;

Struct sockaddr_in server;

If (argc! = 2)

{

Printf ("Usage: % s <IP Address> \ n", argv [0]);

Exit (1 );

}

If (he = gethostbyname (argv [1]) = NULL)

{

Printf ("gethostbyname error. \ n ");

Exit (1 );

}

If (fd = socket (AF_INET, SOCK_STREAM, 0) =-1)

{

Perror ("socket () error. \ n ");

Exit (1 );

}

Bzero (& server, sizeof (server ));

Server. sin_family = AF_INET;

Server. sin_port = htons (PORT );

Server. sin_addr = * (struct in_addr *) he-> h_addr );

If (connect (fd, (struct sockaddr *) & server, sizeof (struct sockaddr) =-1)

{

Perror ("connect () error. \ n ");

Exit (1 );

}

Process (stdin, fd );

Close (fd );

}

Void process (FILE * fp, int sockfd)

{

Char sendbuf [MAXDATASIZE];

Char recvbuf [MAXDATASIZE];

Int num;

Printf ("Input your name: \ n ");

If (fgets (sendbuf, MAXDATASIZE, fp) = NULL)

{

Printf ("lease enter your name, now you have exit. \ n ");

Return;

}

Send (sockfd, sendbuf, strlen (sendbuf), 0 );

While (getMessage (sendbuf, MAXDATASIZE, fp )! = NULL)

{

Send (sockfd, sendbuf, strlen (sendbuf), 0 );

If (num = recv (sockfd, recvbuf, MAXDATASI ...... the remaining full text>

One C # multi-thread Implementation of tcp scoket Programming

Zhidao.baidu.com/..4.html

My previous answer.
Only the server has no client, which is for reference only.

The idea is to use a specific Identifier (string I used) to identify what client each socket is. Then, when you need to forward messages, you can find the client from which the socket receives the data, from the "Sending target" in the message, find the socket from which the message will be sent, and then send it.

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.