Detailed TCP programming functions and steps

  
 

The general server-side steps for TCP programming are

1, create a socket, use the function socket();

2, set the socket property, use the functions setsockopt(); * optional

3. Bind the IP address, port and other information to the socket, use the function bind();

4, enable the listener, use the function listen();

5 Receive the connection from the client, use the function accept();

6, send and receive data, use the function send() and recv(), read() and write();

7, close the network connection;

8, close the listener;

TCP programming client general steps are:

1, create a socket, use the function socket ();

2, set the socket property, use the functions setsockopt (); * optional

3, bind the IP address, port and other information to the socket, use the function bind (); * optional

4. Set the IP address and port of the other party to be connected;

5. Connect to the server and use the function connect();

6. Send and receive data. The functions send() and recv(), or read() and write();

7, close the network connection;

the general step of the UDP programming server side is :

1, create a socket, use the function socket();

2, set the socket property, use the functions setsockopt();* optional

3, bind IP address, port and other information to the socket, use the function bind()

4, cyclically receive data, use the function recvfrom();

5, close the network connection;

1, create a socket, use the function socket();

2, set the socket property, use the functions setsockopt();* optional< Br>

3, bind the IP address, port and other information to the socket, use the function bind(); * optional

4, set the other party's IP address and port and other attributes;

5, send data, use the function sendto ();

6, close the network connection;

commonly used network commands: netstat command netstat is used to display the network connection, routing table and Interface statistics and other network information. Netstat has many options. Our commonly used option is -an to display detailed network status. As for other options we can use help

telnettelnet is a program for remote control But we can use this completely A program to debug our server program. For example, our server program is listening on port 8888, we can use telnet localhost 8888 to check the status of the server

data structure 1, address structure struct sockaddr_in { short int sin_family The /* address family is generally AF-INET */unsigned short int sin_port; /* port number */struct in_addr sin_addr; /* IP address */unsigned char sin_zero[8]; /* padding 0 to remain the same as struct sockaddr Size is filled with memset() or bzero() */};

struct sockaddr { unsigned short sa_family; /* address family, AF_xxx */char sa_data[14]; /* 14-byte protocol address */

};

2, DNS structure struct hostent { char *h_name; /* host's official domain name */char **h_aliases; /* A NULL-terminated host alias array */int h_addrtype; /* The type of address returned, in the Internet environment, AF-INET */int h_length; /* the byte length of the address */char **h_addr_list; /* An array ending in 0, containing the All addresses of the host */};

Operating Functions

1, sock Et:

Create a socket descriptor

int socket(int domain, int type, int protocol); domain=AF_INET, type of socket, type=SOCK_STREAM or SOCK_DGRAM, respectively, representing TCP connections and UDP connection; protocol=0.

Returns an integer socket descriptor.

2.bind:

Associating a socket descriptor with a port on your local machine (for servers only)

int bind(int sockfd,struct sockaddr *my_addr, int addrlen);

Sockfd is a socket descriptor. my_addr is a pointer to the sockaddr type containing information such as the local IP address and port number; addrlen=sizeof(struct sockaddr). Returns: success = 0; failure = -1, errno = error number. You can use the following assignment to automatically get the local IP address and randomly obtain an unoccupied port number: my_addr.sin_port = 0; /* The system randomly selects an unused port number */my_addr.sin_addr.s_addr = INADDR_ANY; /* Fill in the local IP address */

3.Connect:

Create a TCP connection with the remote server (for the client) int connect(int sockfd, struct sockaddr *serv_addr , int addrlen); Sockfd is the sockt descriptor of the destination server serv_addr is a pointer containing the destination IP address and port number. Returns: success = 0; failure = -1, errno = error number.

4, Listen:

Listen whether there is a service request, int listen(int sockfd, int backlog) after bind(); Sockfd is the socket descriptor returned by the Socket system call; backlog Specifies the maximum number of requests allowed in the request queue. The default is 20. Returns: success = 0; failure = -1, errno = error number.

5.accept:

Accepting requests from clients int accept(int sockfd, void *addr, int *addrlen); sockfd is the listened socket descriptor, addr is pointing to the sockaddr_in variable Pointer that holds information about the client's host; addrten points to an integer pointer variable of sizeof(struct sockaddr_in). Returns: A new socket descriptor was successfully returned for this new connection to use. Returns an -1 when the error occurs and sets the corresponding errno value.

6.Send:

In the socket (TCP) socket mode to send information int send (int sockfd, const void *msg, int len, int flags); Sockfd is used to transmit The socket descriptor msg of the data is a pointer to the data to be sent. Len is the length of the data in bytes. Flags are normally set to 0.

7.recv:

Receive data in socket (TCP) socket mode

int recv(int sockfd, void *buf, int len, unsigned int flags) Sockfd is the socket descriptor that accepts data; buf is the buffer that holds the received data; len is the length of the buffer. Flags are also set to zero. Returns: the number of bytes actually received, or 0 if the connection is aborted. When an error occurs, return -1 and set the corresponding errno value.

8.sendto:

Send data in a connectionless (UDP) socket mode. int sendto(int sockfd, const void *msg, int len, unsigned int flags, const struct sockaddr *to, int tolen); to indicates the IP address and port number information of the destination machine tolen=sizeof (struct sockaddr). Returns: the length of the data byte actually sent or -1 if a transmission error occurs.

9.Recvfrom()

Receive data in socketless (UDP) socket mode int recvfrom(int sockfd, void *buf, int len, unsigned int flags, struct sockaddr *from , int *fromlen); from save the source IP address and port number. Fromlen=sizeof(struct sockaddr). Returns: the number of data bytes actually stored in from. Returns -1 when an error occurs and sets the corresponding errno.

10.close()

Release socket, stop any data operation close(sockfd);

11.shutdown:

One-way close connection int Shutdown(int sockfd, int how); how can be set to the following values: ·0-------Do not continue to receive data · 1-------Do not allow to continue sending data ·2- ------Do not allow to continue sending and receiving data. If allowed, call close() shutdown to return 0 when the operation succeeds, and -1 when the error occurs (concatenate the corresponding errno).

12. gethostbyname:

Conversion of domain name and IP address struct hostent *gethostbyname(const char *name);

13.inet_pton function:

Converts a dotted decimal string to a network byte order binary value. This function can handle both IPv4 and IPv6 addresses. Int inet_pton(int family, const char * strptr, void * addrptr);

The first parameter can be AF_INET or AF_INET6: the second parameter is a pointer to a dotted decimal string: the third parameter Is a pointer to the binary value of the converted network endian. Returns: 1---successful 0---the input is not a valid expression format-1---Failure

14.inet_ntop function:

The opposite of the inet_pton function, the inet_ntop function will The network byte order binary value is converted to a dotted decimal string. Const char * inet_ntop(int family,const void * addrptr,char * strptr,size_t len);

The first parameter can be AF_INET or AF_INET6: the second parameter is a binary pointing to the network byte order The pointer to the value; the third argument is a pointer to the converted dotted decimal string; the fourth argument is the size of the target so that the function does not overflow its caller's buffer. Returns: a pointer to the result --- success NULL --- failed



Copyright © Windows knowledge All Rights Reserved