5.10TCP Client Server

Source: Internet
Author: User
Tags socket error htons

    1. Experiment 4-4:TCP Client Server
    • Experimental Purpose:

Familiarity with TCP client server programming

?

    • Experimental requirements:

1. Implement TCP Client Server Echo program

2, transmission information requirements are as follows

(1) Try to send or receive a packet in C language structure

(2) Run the server on the Development Board, the customer service side runs in Ubuntu 12.04, Packet Analysis TCP protocol

?

    • Experimental steps:

1. Server-side

#include <stdlib.h>?

#include <stdio.h>?

#include <errno.h>?

#include <string.h>?

#include <netdb.h>?

#include <sys/types.h>?

#include <netinet/in.h>?

#include <sys/socket.h>?

?

#define PortNumber 3333//The port number of the server and the port number of the client must be equal

?

int main (int argc, char *argv[])?

{?

int sockfd,new_fd;?

struct sockaddr_in server_addr;?

struct sockaddr_in client_addr;?

int sin_size;?

int nbytes;

Char buffer[1024];

?

?

/* Start building SOCKFD descriptors on server side */?

if ((Sockfd=socket (af_inet,sock_stream,0)) ==-1)//Af_inet:ipv4; Sock_stream:tcp

{?

fprintf (stderr, "Socket error:%s\n\a", Strerror (errno));?

Exit (1);?

}?

?

/* Server-side populated sockaddr structure */?

Bzero (&server_addr,sizeof (struct sockaddr_in)); Initialize, set 0

server_addr.sin_family=af_inet;???????? Internet

Server_addr.sin_addr.s_addr=htonl (Inaddr_any);//(Convert Long data on this machine to long data on the network) and any host communication?//inaddr_any Represents data that can receive any IP address, which is bound to all IP

Server_addr.sin_port=htons (portnumber);???? (Convert short data on this machine to short data on the network) port number

?

/* Bundle SOCKFD Descriptor to IP address */?

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

{?

fprintf (stderr, "Bind error:%s\n\a", Strerror (errno));?

Exit (1);?

}?

?

/* Set the maximum number of clients allowed to connect */?

if (Listen (sockfd,5) ==-1)?

{?

fprintf (stderr, "Listen error:%s\n\a", Strerror (errno));?

Exit (1);?

}?

?

while (1)?

{?

/* Server blocked until client program establishes a connection */?

sin_size=sizeof (struct sockaddr_in);?

if ((New_fd=accept (SOCKFD, (struct sockaddr *) (&CLIENT_ADDR), &sin_size)) ==-1)?

{?

fprintf (stderr, "Accept error:%s\n\a", Strerror (errno));?

Exit (1);?

}?

fprintf (stderr, "Server get connection from%s\n", Inet_ntoa (CLIENT_ADDR.SIN_ADDR)); Converts a network address into a. String

?

if ((Nbytes=read (new_fd,buffer,1024)) ==-1)//Here can be written as if ((Nbytes=recv (new_fd,buffer,1024,0)) ==-1)?

{?

fprintf (stderr, "Read error:%s\n", Strerror (errno));?

Exit (1);?

}

buffer[nbytes]= ' + ';

printf ("Server received%s\n", buffer);

?

/* This communication is over.

Close (NEW_FD);?

/* Cycle the next one */?

}?

?

/* End communication */?

Close (SOCKFD);?

Exit (0);?

}?

?

???? 2. Send student information on client and receive echo result

#include <stdlib.h>?

#include <stdio.h>?

#include <errno.h>?

#include <string.h>?

#include <netdb.h>?

#include <sys/types.h>?

#include <netinet/in.h>?

#include <sys/socket.h>?

?

#define PortNumber 3333

?

int main (int argc, char *argv[])?

{?

int sockfd;?

Char buffer[1024];?

struct sockaddr_in server_addr;?

struct hostent *host;?

?

? ? ? ? /* Use hostname to query the host name */

if (argc!=2)?

{?

fprintf (stderr, "usage:%s hostname \a\n", argv[0]);?

Exit (1);?

}?

?

if ((Host=gethostbyname (argv[1))) ==null)?

{?

fprintf (stderr, "GetHostName error\n");?

Exit (1);?

}?

?

/* Client starts to build the SOCKFD descriptor */?

if ((Sockfd=socket (af_inet,sock_stream,0)) ==-1)//af_inet:internet; Sock_stream:tcp

{?

fprintf (stderr, "Socket error:%s\a\n", Strerror (errno));?

Exit (1);?

}?

?

/* Client program fills the data on the server * *?

Bzero (&server_addr,sizeof (SERVER_ADDR)); Initialize, set 0

server_addr.sin_family=af_inet;?????//IPV4

Server_addr.sin_port=htons (portnumber);//(Convert short data on this machine to short data on the network) port number

server_addr.sin_addr=* (struct in_addr *) host->h_addr); IP Address

?

/* Client Initiating connection request */?

if (Connect (SOCKFD, struct sockaddr *) (&SERVER_ADDR), sizeof (struct sockaddr)) ==-1)?

{?

fprintf (stderr, "Connect error:%s\a\n", Strerror (errno));?

Exit (1);?

}?

?

/* Connection succeeded */?

printf ("Please input char:\n");

?

/* Send data */

Fgets (Buffer,1024,stdin);?

Write (Sockfd,buffer,strlen (buffer)); This can be written as send (Sockfd,buffer,strlen (buffer), 0);

?

/* End communication */?

Close (SOCKFD);?

Exit (0);?

}?

?

?

?

3, the server receives information, display and Echo

?

???? 4. Cross-Compile the server and run it through NFS shared copy to the Development Board

?

5, compile the customer service side, directly on Ubuntu 12.04 run

?

6, the test client and server can transfer student information through TCP normally

?

?

?

?

?

?

?

IP for server and client:

?

Experiment Experience:

???? is a TCP type of connection-based communication, which implements a server-side: Development Board, and a client PC host. The exchange of information between the two. The above is my realization. The whole process was very smooth. Thank you.

5.10TCP Client Server

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.