Socket programming under Linux

Source: Internet
Author: User

1. Create a socket

int LISTENFD;LISTENFD = socket (af_inet, sock_stream, 0); Streaming sockets if (LISTENFD < 0) {  perror ("socket");  Exit (1);    }

2. Bind bind

structsockaddr_in serv_addr, Cli_addr;bzero (&AMP;SERV_ADDR,sizeof(SERV_ADDR)); Sin.sin_family=Af_inet;sin.sin_port= Htons (6363);//Port numberSIN.SIN_ADDR.S_ADDR = htonl (Inaddr_any);//IP Address//Avoid binding not succeedingintOn =1; setsockopt (SOCKFD, Sol_socket, SO_REUSEADDR,&on,sizeof(int));if(Bind (LISTENFD, (structSOCKADDR *) &serv_addr,sizeof(SERV_ADDR)) == -1) {perror ("bind failed.\n"); Exit (1);} 

3. Monitoring Listen

if (Listen (LISTENFD, n) = =-1//  N represents the number of listeners {    perror ("Listen failed\n");    Exit (1);}

4.accept

intclientfd;//struct sockaddr_in cli_addr;intLen =sizeof(structsockaddr); Clientfd= Accept (SERVERFD, (structsockaddr*) &cli_addr, &len);if(Clientfd = =-1) {perror ("Accept failed\n"); Exit (1);}Elseprintf ("Accept succeed\n");

5. Connecting to connect

intsockfd;structsockaddr_in serv_addr;host= GetHostByName ("127.0.0.1"); SOCKFD= Socket (Af_inet, Sock_stream,0); Bzero (&AMP;SERV_ADDR,sizeof(structsockaddr)); Serv_addr.sin_family=Af_inet;serv_addr.sin_port= Htons (port);//Port Port NumberSERV_ADDR.SIN_ADDR = * ((structIN_ADDR *) host->h_addr);if(Connect (SOCKFD,structSOCKADDR *) &serv_addr,sizeof(structSOCKADDR)) = =-1) {printf ("Connect failed\n"); Exit (1);}Elseprintf ("connect succeed \ n");

6. Sending data Send//TCP

0);

7. Receiving data recv//TCP

intRecvnum;CharBuf[buf_size];//Define buf_size 1024x768if(Recvnum = recv (SOCKFD, buf, Buf_size,0)) >0) {printf ("recv:%s\n", buf);}Else if(Recvnum = =-1) {perror ("Recv-1"); Exit (1);}

8. Create a thread

1. Add header file # include<pthread.h>2. Adding parameters at compile time gcc-G xxx.c-o xxx-pthread3. pthread_t pid;intSockfd;pthread_create (&pid, NULL, (void*) Recv_data, (void*) &SOCKFD); of which:void*recv_data (void*FD) {  intSOCKFD = * ((int*) FD); ...     while(1) {recv (...); }  }

 

Socket programming under Linux

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.