Linux Socket Learning

Source: Internet
Author: User
Tags socket error htons

1 //server-side server.c2#include <stdio.h>3#include <stdlib.h>4#include <string.h>5#include <errno.h>6#include <sys/types.h>7#include <sys/socket.h>8#include <netinet/inch.h>9 #defineMAXLINE 4096Ten  One //socket (int domain, int type, int protocol) A //protocol Domain socket type Specify protocol - intMainintargcChar**argv) - { the     intN, LISTENFD, CONNFD; -     structsockaddr_in servaddr; -     CharBuff[maxline]; -     if(LISTENFD = socket (af_inet, Sock_stream,0)) == -1 ) +     { -printf"Create socket error:%s (error:%d) \ n", Strerror (errno), errno); +Exit0); A     } atmemset (&AMP;SERVADDR,0,sizeof(SERVADDR)); -servaddr.sin_family =af_inet; -SERVADDR.SIN_ADDR.S_ADDR = htonl (Inaddr_any);//the system binds the default network card as the IP address -Servaddr.sin_port = htons (6666);//listen to the port number No. 6666 on this machine. -  -     if(Bind (LISTENFD, (structsockaddr*) &servaddr,sizeof(SERVADDR)) == -1) in{//assign a specific address in the address family to the socket -printf"bind socket Error:%s (errno:%d) \ n", Strerror (errno), errno); toExit0); +     } -     if(Listen (LISTENFD,Ten) == -1) the{//Call Listen () to listen for sockets *printf"Listen socket Error:%s (errno:%d) \ n", Strerror (errno), errno); $Exit0);Panax Notoginseng     } -printf"waiting for client ' s request...\n"); the      while(1) +     { A         if(CONNFD = Accept (LISTENFD, (structsockaddr*) (null)) = =-1) the{//call the Accept () function to receive the request, the connection is established. +printf"Accept Socket Error:%s (errno:%d)", Strerror (errno), errno); -             Continue; $         } $n = recv (CONNFD, Buff, MAXLINE,0);//The recv function returns the number of bytes of its actual copy -Buff[n] =' /'; -printf"recv msg from client:%s\n", buff); the Close (CONNFD); -     }Wuyi Close (LISTENFD); the}
1 //Client client.c2#include <stdio.h>3#include <stdlib.h>4#include <string.h>5#include <errno.h>6#include <sys/types.h>7#include <sys/socket.h>8#include <netinet/inch.h>9 #defineMAXLINE 4096Ten  One intMainintargcChar**argv) A { -     intN, sockfd; -     CharRecvline[maxline], sendline[maxline]; the     structsockaddr_in servaddr; -     if(ARGC! =2) -     { -printf"usage:./client <ipaddress>\n"); +Exit0); -     } +     if(SOCKFD = socket (af_inet, Sock_stream,0)) <0) A     { atprintf"Create socket Error:%s (errno:%d) \ n", Strerror (errno), errno); -Exit0); -     } -memset (&AMP;SERVADDR,0,sizeof(SERVADDR)); -servaddr.sin_family =af_inet; -Servaddr.sin_port = htons (6666); in  -     if(Inet_pton (Af_inet, argv[1], &servaddr.sin_addr) <=0) to{//IP address translation function +printf"Inet_pton error for%s\n", argv[1]); -Exit0); the     } *     if(Connect (SOCKFD,structsockaddr*) &servaddr,sizeof(SERVADDR)) <0) ${//call Connect () to make a connection requestPanax Notoginsengprintf"Connect Error:%s (errno:%d) \ n", Strerror (errno), errno); -Exit0); the     } +printf"send msg to server: \ n"); AFgets (Sendline,4096, stdin); the     if(Send (SOCKFD, Sendline, strlen (Sendline),0) <0) +     { -printf"Send msg Error:%s (errno:%d) \ n", Strerror (errno), errno); $Exit0); $     } - Close (SOCKFD); -Exit0); the}

After compiling with GCC respectively, execute, open 2 terminals:

Linux Socket Learning

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.