Communication between TCP network programming under Linux and C#socket programming based on Windows

Source: Internet
Author: User
Tags htons

First, the Linux under the TCP Network Programming foundation, needs to understand the correlation function

Socket (): for socket initialization.

Bind (): The socket is bound to a port on this computer, and the service request can be monitored on that port.

Listen (): Causes the socket to be in passive listening mode and creates an input for the socket

Data queues, which will arrive at the server, are requested to be saved in this queue until the program processes them.

Accept (): Let the server receive the client's connection request.

Connect (): The client uses the Connect function to configure the socket and establish a TCP connection to the remote server.

Close (): Close socket

Send (): Sending function

RECV (): Accept function

Second, the server and client flowchart as shown

Third, Linux under the TCP programming example

TCP Server Programs

/* SERVER.C */

#include <sys/types.h>

#include <sys/socket.h>

#include <sys/ioctl.h>

#include <netinet/in.h>

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <unistd.h>

#define PORT 2000//define port number

#define BUFFER_SIZE 1024

#define MAX_QUE_CONN_NM 5//MAX buffer queue

int main (void)

{

/* The Ethernet socket address structure is very important below */

struct sockaddr_in server_addr,client_addr;

int sin_size,recvbytes,wbytes;

int ser_fd,cli_fd;

Char Buf[buffer_size];

/* Establish socket connection, IPV4 protocol, BYTE stream socket */

if ((SER_FD = socket (af_inet,sock_stream,0)) = =-1)

{

Perror ("socket");

Exit (1);

}

printf ("Socket id =%d\n", ser_fd);

/* Initialize the sockaddr_in struct */

server_addr.sin_family = af_inet;

Server_addr.sin_port = htons (port);

SERVER_ADDR.SIN_ADDR.S_ADDR = Inaddr_any;

Bzero (& (Server_addr.sin_zero), 8);

/* bind function Bind */

if (bind (SER_FD, struct sockaddr *) &server_addr,sizeof (struct

SOCKADDR)) ==-1)

{

Perror ("bind");

Exit (1);

}

printf ("Bind success!\n");

/* Call the Listen function to listen */

if (listen (ser_fd,max_que_conn_nm) = =-1)

{

Perror ("Listen");

Exit (1);

}

printf ("listening......\n");

/* Call the Accept function to wait for the client's connection */

if ((cli_fd = Accept (ser_fd, (struct sockaddr *) &client_addr,&sin_size)) ==-1)

{

Perror ("accept");

Exit (1);

}

printf ("Has client ready for connecting\n");

/* Call the RECV function to receive the client's request */

memset (buf,0,sizeof (BUF));

if ((Recvbytes = recv (cli_fd,buf,buffer_size,0)) = = =-1)

{

Perror ("recv");

Exit (1);

}

/* Print out the information you received (information sent from customer service) */

printf ("Received a message:%s\n", buf);

/* Processing the data sent by the client, just multibyte the first word to 2, then send to the client.

buf[0]=buf[0]+2;

if ((wbytes = Write (Cli_fd,buf,strlen (BUF))) = = =-1)

{

Perror ("Handle send");

Exit (1);

}

Else

printf ("Handle buf is%s\n", buf);

/* Close Socket */

Close (SER_FD);

return 0;

}

TCP Client Programs

/*client*/

#include <sys/types.h>

#include <sys/socket.h>

#include <sys/ioctl.h>

#include <netinet/in.h>

#include <netdb.h>

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <unistd.h>

#define PORT 2000//Port number

#define BUFFER_SIZE 1024

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

{

struct sockaddr_in server_addr;

int sockfd,sendbytes,rbytes;

int ser_fd;

Char Buf[buffer_size];

struct Hostent *host;

/* Specifies an input parameter of 3, otherwise error */

if (argc!=3)

{

Perror ("Usage:./clinet IP address text\n");

Exit (1);

}

/* Address Resolution function */

if (host = gethostbyname (argv[1)) = = = NULL)//Get host name and corresponding information

{

Perror ("gethostbyname");

Exit (1);

}

memset (buf,0,sizeof (BUF));

sprintf (buf, "%s", argv[2]);

Buf[strlen (BUF) +1]= ' + ';

/* Establish socket connection, IPV4 protocol, BYTE stream socket */

if ((SOCKFD = socket (af_inet,sock_stream,0)) = =-1)

{

Perror ("socket");

Exit (1);

}

printf ("Socket id =%d\n", SOCKFD);

/* Initialize the sockaddr_in struct */

server_addr.sin_family = af_inet; TCP/IP protocol cluster

Server_addr.sin_port = htons (port); Sin_port Storage port number (using network byte order) htons convert unsigned short from host byte order to network byte order

server_addr.sin_addr = * (struct in_addr *) host->h_addr); A pointer to the host name and the corresponding information, which is the IP address, as previously indicated

Bzero (& (Server_addr.sin_zero), 8); Equivalent to memset, emptying function

/* Call the Connect function to actively initiate a connection to the server */

if (Connect (SOCKFD, struct sockaddr *) &server_addr,sizeof (struct sockaddr)) = =-1)//2nd parameter, pointing to the SOCKADDR structure to which the socket is to be connected The pointer

{

Perror ("Connect");

Exit (1);

}

printf ("Connect server success!\n");

/* Send message to server side */

if (sendbytes = Send (Sockfd,buf,strlen (BUF), 0) = = =-1)

{

Perror ("send");

Exit (1);

}

else printf ("BUF is%s\n", buf);

/* read out the server's processed data */

if ((Rbytes=read (sockfd,buf,100)) ==-1)

{

printf ("Read handle error\n");

Exit (0);

}

Else

printf ("read handle buf is%s \ n", buf);

Close (SOCKFD);

return 0;

}

Iv. the results of the program operation are as follows:

Client Results

Server results

Five, C#socket () programming

Reference: http://lanxicy.com/read/9740d8d480de02ad528ada5d.html

Some modifications were made to the data to generate EXE files

The operation results are as follows

C#TCP Client

C#TCP Server

Because TCP network programming is used under Linux and C #, Linux is used as a client, C # as a server, the port number and IP address are set.

They can communicate with each other, and then in C # can write the relevant data into the database, the relevant pages and then read from the database in the relevant data on the line.

The operation results are as follows

Linux Client

C # Server

Vii. Summary of the experience

Network programming under Linux and Windows C # Network programming are all communicating using the TCP protocol, so even across platforms, they can communicate.

Description: Reprint please indicate the source!

Communication between TCP network programming under Linux and C#socket programming based on Windows

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.