Accept function in socket

Source: Internet
Author: User

The Problem and Solution I met I wrote a socket program last night. Because my client is a one-time program, the simplest server and client will be automatically launched once the server is connected, then run again. /client before I can connect to the server again. I found that when the first client connects to the server, the client address printed by the server is not 127.0.0.1, which is wrong! The port number is 1023. For example, I thought it was caused by the fact that the sockaddr_in struct of the user accept function in my server was not initialized. Then I added bzero (& cin, sizeof (cin )); the statement is used for the initialization of the cin struct. Then I find that the first connection is 0.0.0.0, and the port is also 0! Why? Baidu once found that a person's socket program before calling the accept function, first assign the value of len = sizeof (cliaddr) to the parameter len required by the accept function; then call acceptfc = 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! The accept function checks my current system (Linux 2.6.35.14-106. fc14.i686) header file in/Usr/include/Sys/socket. h defines the accept function [cpp]/* 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 Cancellation point and therefore not marked with _ THROW. */extern int accept (int _ fd, _ SOCKADDR_ARG _ addr, socklen_t * _ restrict _ addr_len ); someone found on the Internet that the last parameter of the accept function can be NULL. I tested the Code as follows: [cpp] 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! (May be caused by different system versions) my server. c [cpp] # 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 _ Port = 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 ;}

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.