Socket programming under Linux

Source: Internet
Author: User
Tags socket error htons

Network communication programming is to write through the computer and other programs to communicate between the program, the program of mutual communication can be called the client program, the other party is called the Service program, the application system provides socket programming interface can write their own network program.

A transfer via TCP/IP protocol

TCP: Provides a reliable communication connection for the application. Suitable for one-time transmission of large numbers of data conditions. and is used to request a response program that is obtained.

UDP: Provides wireless connection communication, and the transmission packet reliability guarantee. Suitable for transmitting small amounts of data at a time, reliability is the responsibility of the application layer.

Two socket sockets

Network communication programming is done through the socket interface. The socket interface is a TCP/IP network API that includes a full set of invocation interfaces and data structure definitions that provide the application with the means to communicate using network protocols such as TCP/UDP.

Each socket is represented by a semi-related description (protocol, local address, local port), and a complete socket is represented by a related description (protocol, local address, local port, remote address, remote port).

There are 3 types of sockets that are common:

    1. Streaming socket sockets (SOCK_STREAM)

Streaming sockets provide connection-oriented, reliable data transfer services that are error-free, non-repetitive, and receive in the order in which they are sent.

2. Datagram Socket Socke (SOCK_DGRSM)

Datagram sockets define a non-connected service that transmits data through separate messages, is unordered, and is not guaranteed to be reliable and error-free.

3 RAW sockets

Allows direct access to underlying protocols such as IP or ICMP, which is inconvenient for powerful use, mainly for the development of some protocols.


Three Customer/Service models

In TCP/IP network applications, the main mode of interaction between the two processes of communication is the client/server, and the client requests to the server, and the server receives the request and provides the corresponding service.



Programming process

Server side: Socket-bind-listen-accept-recv/recvfrom-send/sendto-close


#include <sys/types.h>

#include <sys/socket.h>

#include <stdio.h>

#include <stdlib.h>

#include <errno.h>

#include <string.h>

#include <unistd.h>

#include <netinet/in.h>


#define PORT 4321

#define BUFFER_SIZE 4024

#define MAX_QUE_CONN_NM 5


int main ()

{

struct sockaddr_in server_addr,client_addr;

int sockfd,recvbytes;

int sin_size,client_fd;

Char Buffer[buffer_size];

if ((Sockfd=socket (af_inet,sock_stream,0)) ==-1)

{

Perror ("socket");

Exit (1);

}

Server_addr.sin_family=af_inet;

Server_addr.sin_port=htons (port);

Server_addr.sin_addr.s_addr=inaddr_any;

Bzero (& (Server_addr.sin_zero), 8);

int i=1;

SetSockOpt (sockfd,sol_socket,so_reuseaddr,&i,sizeof (i));

if (bind (SOCKFD, struct sockaddr *) &server_addr,sizeof (struct sockaddr)) ==-1)

{

Perror ("bind\n");

Exit (1);

}

printf ("Bind success\n");

if (Listen (SOCKFD,MAX_QUE_CONN_NM) ==-1)

{

Perror ("listen\n");

Exit (1);

}

printf ("listen......\n");

if ((Client_fd=accept (SOCKFD, (struct sockaddr *) &client_addr,&sin_size) ==-1)

{

Perror ("recv\n");

Exit (1);

}

printf ("Server Get connection from%s\n", (char *) Inet_ntoa (CLIENT_ADDR.SIN_ADDR));


memset (buffer,0,sizeof (buffer));

if ((Recvbytes=recv (client_fd,buffer,buffer_size,0)) ==-1)

{

Perror ("recv");

Exit (1);

}




Char *str1= "Hello server";

Char *str2= "Hello ABC";

if (strncmp (Buffer,str1,strlen (str1)) ==0)

{

printf ("Receive a message:%s\n", buffer);

Send (CLIENT_FD, "Hello client", 12,0);

printf ("Send a Message:hello client\n");

}

if (strncmp (buffer, "exit", 4) ==0)

{

Exit (0);

Close (SOCKFD);

}

}



Client: Socket-connect-send/sendto-recv/recvfrom-close



#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <errno.h>

#include <sys/types.h>

#include <sys/socket.h>

#include <netinet/in.h>

#define MAXLINE 4096

int main (int argc, char** argv)

{

int SOCKFD, N,rec_len;

Char recvline[4096], sendline[4096];

Char Buf[maxline];

struct sockaddr_in servaddr;

if (argc! = 2) {

printf ("Usage:./client <ipaddress>\n");

Exit (0);

}

if ((SOCKFD = socket (af_inet, sock_stream, 0)) < 0) {

printf ("Create Socket Error:%s (errno:%d) \ n", Strerror (errno), errno);

Exit (0);

}

memset (&servaddr, 0, sizeof (SERVADDR));

servaddr.sin_family = af_inet;

Servaddr.sin_port = htons (4321);

if (Inet_pton (Af_inet, argv[1], &servaddr.sin_addr) <= 0) {

printf ("Inet_pton error for%s\n", argv[1]);

Exit (0);

}

if (Connect (SOCKFD, (struct sockaddr*) &servaddr, sizeof (SERVADDR)) < 0) {

printf ("Connect Error:%s (errno:%d) \ n", Strerror (errno), errno);

Exit (0);

}

printf ("Send msg to server: \ n");

Fgets (Sendline, 4096, stdin);

if (Send (SOCKFD, Sendline, strlen (Sendline), 0) < 0)

{

printf ("Send msg Error:%s (errno:%d) \ n", Strerror (errno), errno);

Exit (0);

}

if (Rec_len = recv (SOCKFD, buf, maxline,0) = = =-1) {

Perror ("recv error");

Exit (1);

}

Buf[rec_len] = ' + ';

printf ("Received:%s", buf);

Close (SOCKFD);

Exit (0);

}













This article is from "hem" blog, please make sure to keep this source http://10706021.blog.51cto.com/10696021/1788233

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.