Linux C-TCP data send and receive sample

Source: Internet
Author: User
Tags htons

1.client End (Read)

#include <stdio.h>#include<stdlib.h>#include<string.h>#include<unistd.h>#include<arpa/inet.h>#include<sys/socket.h>voidError_handling (Char*msg);intMainintargcChar*argv[]) {    intsock_fd; structsockaddr_in serv_addr; Charmessage[ -] = {0}; intRcv_len; if(ARGC! =3) {printf ("Usage:%s <IP> <port>\n", argv[0]); Exit (1); } sock_fd= Socket (Af_inet, Sock_stream,0); if(SOCK_FD = =-1) error_handling ("sock () error"); memset (&AMP;SERV_ADDR,0,sizeof(SERV_ADDR)); Serv_addr.sin_family=af_inet; Serv_addr.sin_addr.s_addr= Inet_addr (argv[1]);//server ' s IP addrServ_addr.sin_port = htons (Atoi (argv[2]));//Port    if(Connect (SOCK_FD,structsockaddr*) &serv_addr,sizeof(SERV_ADDR)) == -1) error_handling ("Connect () error!"); Rcv_len= Read (SOCK_FD, message,sizeof(message)-1); if(Rcv_len = =-1) error_handling ("read () error!"); printf ("Message from server:%s\n", message); printf ("Read length:%d\n", Rcv_len);    Close (SOCK_FD); return 0;}voidError_handling (Char*message)    {fputs (message, stderr); FPUTC ('\ n', stderr); Exit (1);}

If you are sending data in the client, simply modify read to write.

2.server (send)

#include <stdio.h>#include<stdlib.h>#include<string.h>#include<unistd.h>#include<arpa/inet.h>#include<sys/socket.h>voidError_handling (Char*msg);intMainintargcChar*argv[]) {    intserv_sock_fd; intclnt_sock_fd; structsockaddr_in serv_addr; structsockaddr_in clnt_addr;    Socklen_t clnt_addr_size; CharMessage[] ="This is a sample tcp?sserver!"; if(ARGC! =2) {printf ("Usage:%s <port>\n", argv[0]); Exit (1); } serv_sock_fd= Socket (Af_inet, Sock_stream,0); if(SERV_SOCK_FD = =-1) error_handling ("sock () error"); memset (&AMP;SERV_ADDR,0,sizeof(SERV_ADDR)); Serv_addr.sin_family=af_inet; Serv_addr.sin_addr.s_addr=htonl (Inaddr_any); Serv_addr.sin_port= Htons (Atoi (argv[1])); if(Bind (SERV_SOCK_FD, (structsockaddr*) &serv_addr,sizeof(SERV_ADDR)) == -1) error_handling ("bind () error"); if(Listen (SERV_SOCK_FD,5) == -1) error_handling ("Listen () Error"); Clnt_addr_size=sizeof(CLNT_ADDR); CLNT_SOCK_FD= Accept (SERV_SOCK_FD, (structsockaddr*) &clnt_addr, &clnt_addr_size); if(CLNT_SOCK_FD = =-1) error_handling ("Accept () error"); Write (CLNT_SOCK_FD, message,sizeof(message));    Close (CLNT_SOCK_FD);    Close (SERV_SOCK_FD); return 0;}voidError_handling (Char*message)    {fputs (message, stderr); FPUTC ('\ n', stderr); Exit (1);}

If you are receiving data on the server side, simply modify write to read.

3, above, under Linux separately GCC compiles, runs the server program first, then runs the client program, the server side waits for the client connection, once the connection and completes the data sends, the server shuts down.

4, the server of the sample program can only connect to one client side and does not verify the correctness of the data sending and receiving.

Linux C-TCP data send and receive sample

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.