Accept function in socket

Source: Internet
Author: User
Problems and Solutions

I wrote a socket program last night, the simplest server and client.

Because my client is a one-time program, once connected to the server, it will be automatically released, and then re-run./client before connecting to the server again

I found that when the client connects to the server for the first time, the client address printed by the server is not 127.0.0.1, which is incorrect! The port number is 1023, for example:

 

At first, I thought it was because the sockaddr_in struct of the user accept function on my server was not initialized. Later, I added bzero (& Cin, sizeof (CIN). The statement is used for the initialization of the CIN struct.

Then I found that the first connection changed to 0.0.0.0, and the port is also 0! Why?

Baidu only found that a person's socket program assigned a value to the parameter Len required by the accept function before calling the accept function.

Len = sizeof (cliaddr );

And then call accept.

Fc = accept (listenfd, (struct sockaddr *) & Cin, & Len );

It was good after the test! In this way, we can see that my error lies in the incorrect accept function parameter!

 

Accept Function

I checked the header file of my current system (Linux 2.6.35.14-106. fc14.i686) and defined the accept function in/usr/include/sys/socket. h.

/* Await a connection on socket FD.   When a connection arrives,open a new socket to communicate with it,   set *ADDR (which is*ADDR_LEN bytes long) to the address of the connecting   peer and *ADDR_LEN to theaddress's actual length, and return the   new socket's descriptor, or-1 for errors.    This function is acancellation point and therefore not marked with   __THROW.  */extern int accept (int __fd, __SOCKADDR_ARG __addr,           socklen_t*__restrict __addr_len);

I found someone on the internet saying that the last parameter of the accept function can be null, And I tested it. The Code is as follows:

        fc = accept(fs,(struct sockaddr*)&cin , NULL);        if(fc < 0)        {               printf("fc==%d\n",fc);            return -1;         }   

When running, it is found that the running has exited, and Fc =-1 is printed! It can be seen that the last parameter is not null! (This may be caused by different system versions)

 

My server. c
# Include <stdio. h> # include <stdlib. h> # include <string. h> # include <ARPA/inet. h> # define max_line 100 # Define Port 8000 void my_fun (char * P);/* convert uppercase characters to lowercase letters */void my_fun (char * P) {If (P = NULL) return; For (; * P! = '\ 0'; P ++) {If (* P> = 'A' & * P <= 'Z ') {* P = * P-'A' + 'A' ;}} int main (INT argc, char ** argv) {struct sockaddr_in sin; struct sockaddr_in CIN; int FS, FC; // used to create a socket. Each server and client has a socklen_t Len; char Buf [max_line]; char addr_p [inet_addrstrlen]; // used to save the Client IP information int N; bzero (& sin, sizeof (SIN); bzero (& Cin, sizeof (CIN); sin. sin_family = af_inet; sin. sin_addr.s_addr = inaddr_any; // The Server accepts any address sin. sin_por T = htons (port); FS = socket (af_inet, sock_stream, 0); // server socket, tcp bind (FS, (struct sockaddr *) & sin, sizeof (SIN); Listen (FS, 10 ); printf ("========= welcome to Jack's server ===========\ N"); memset (addr_p, 0, sizeof (addr_p); While (1) {Len = sizeof (CIN); // Len must be assigned a value; otherwise, the first client address is 0.0.0.0! Printf ("Len = % d \ n", Len); fc = accept (FS, (struct sockaddr *) & Cin, & Len ); // fc = accept (FS, (struct sockaddr *) & Cin, null); // try not to be null if (FC <0) {printf ("FC = % d \ n", FC); Return-1;} n = read (FC, Buf, max_line ); // The read length is n inet_ntop (af_inet, & cin. sin_addr, addr_p, sizeof (addr_p); printf ("Client IP: % s, Port: % d \ n", addr_p, ntohs (CIN. sin_port); printf ("content: % s \ n", Buf); my_fun (BUF); write (FC, Buf, n); close (FC );} if (close (FS) =-1) {perror ("fail to close \ n"); exit (1) ;}return 1 ;}

Study hard, every day !!

 

 

 

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.