Getting started with Linux socket Programming (1)-TCP server

Source: Internet
Author: User


#include <stdio.h> #include <stdlib.h> #include <string.h> #include <errno.h> #include <sys/ types.h> #include <sys/socket.h> #include <netinet/in.h> #define DEFAULT_PORT 8000 #define MAXLINE 4096 int main (int argc, char **argv) {int listenfd, CONNFD; Socket socket struct SOCKADDR_IN servaddr; Structural body of IP address char buff[maxline]; A bufint response_max_length used to hold data and send data; int result; Determine if each function is turned on successfully LISTENFD = socket (af_inet, sock_stream, 0), if (LISTENFD = =-1) {//If the return value is-1, we think the function is performing an error. Same below printf ("Error:socket failed\n"); return 0;} Bzero (&servaddr, sizeof (SERVADDR)); Initializes the memory (padding 0).
Because the structure, in addition to the useful information needs to fill out,
Other elements/content can be filled with 0. servaddr.sin_family = af_inet; Fill in the necessary information servaddr.sin_addr.s_addr = HTONL (inaddr_any); servaddr.sin_port = Htons (9997); Htons,htonl These functions mean host to online,
is essential when the host is programmed.
function, because here comes the data is big or small end result = Bind (LISTENFD, (struct sockaddr *) &servaddr, sizeof (SERVADDR)); if (result = =-1) {printf ("Error:bind failed\n"); return 0;}
result = Listen (listenfd,10); if (result = =-1) {printf ("Error:listen failed\n"); return 0;} for (;;) Dead Loop {connfd = Accept (LISTENFD, (struct sockaddr *) null, NULL); if (CONNFD = =-1) {printf ("error:accept failed\n"); return 0 ;} for (;;) {response_max_length = recv (CONNFD, Buff, MAXLINE, 0); Buff[response_max_length] = ';p rintf ' ("%s", buff); result = Send ( CONNFD, Buff, 26,0); if (result = =-1) {printf ("error:send failed\n"); return 0;}} Close (CONNFD);} Close (LISTENFD); return 0;}

This is the code for the TCP server. Server function is relatively simple, is to send the client's content printed on the screen, and to the client identical content,

The process of the service side is as follows:

1. Socket (); Get sockets

2. bind (); Bound ports and IP

3. Listen (); Listening port

4. Accept (); Accept client-side visits. the TCP three-time handshake is done here.

5. recv (); Accept the message sent by the client.

6. Send (); Send a message to the client

Explanation of function Parameters:

  int socket (int domain, int type, int protocol);

    The parameters of this function are fixed ( af_inet, Sock_stream, 0). After the socket is created successfully, a value of type int is returned, representing the socket.

  

bind (int sockfd,const struct sockaddr *myaddr,socklen_t addrlen);

    The first parameter is the value returned by the socket (). The second parameter is a struct struct sockaddr that represents an IP address. The third parameter is sizeof (struct sockaddr).

  

int Listen (int sockfd, int backlog);

   Backlog is the maximum number of non-processed connection request queues that can be accommodated.

  int accept (int sockfd, void *addr, int *addrlen);

    The second parameter is a struct-sockaddr pointer to the structure type. Once the connection is successful, you can obtain the client's IP address through this pointer. The third parameter is also sizeof (struct sockaddr). If you do not need to know the IP address of the client side, the second third parameter can write null

  int send (int sockfd, const void *msg, int len, int flags);

  int recv (int sockfd, void *buf, int len, unsigned int flags);

    The second parameter is a character array that is used to send or save the contents of the communication. The third argument is the length of the string. Strlen (BUF). The fourth parameter flags generally write 0, beginners do not have to delve into this.

Getting started with Linux socket Programming (1)-TCP server

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.