Network Programming client and server

Source: Internet
Author: User

After translating so many chapters, we need to write a simple client and server-side program by ourselves!

 


Client Side:

Getaddrinfo ()-> socket ()-> connect ()-> recv ()


Code:
# Include <stdio. h>
# Include <string. h>
# Include <sys/types. h>
# Include <sys/socket. h>
# Include <arpa/inet. h>
# Include <netdb. h>
 
# Define SERVIP "192.168.56.112"
# Define SERVPORT "2349"
 
Int main (int argc, char * argv [])
{
Struct addrinfo hints, * res;
Int status;
Int sockfd;
Int connSta;

Int recvSta;
Char buffer [1024];
Int len;

Char ipstr [INET_ADDRSTRLEN];
Void * addr;

If (argc! = 3 ){
Fprintf (stderr, "Usage: Not Server IP And Port! ");
Return 1;
}

Memset (& hints, 0, sizeof (hints ));
Hints. ai_family = AF_UNSPEC;
Hints. ai_socktype = SOCK_STREAM;

Status = getaddrinfo (argv [1], argv [2], & hints, & res );

If (status! = 0 ){
Fprintf (stderr, "Error, Server IP And Port! ");
Return 2;
}

Sockfd = socket (res-> ai_family, res-> ai_socktype, res-> ai_protocol );

If (sockfd = 0 ){
Fprintf (stderr, "Create, socket fail! ");
Return 3;
}

ConnSta = connect (sockfd, res-> ai_addr, res-> ai_addrlen );

If (connSta! = 0 ){
Fprintf (stderr, "Connect, Remote Host fail! ");
Return 4;
}
Else {
Struct sockaddr_in * ipv4 = (struct sockaddr_in *) res-> ai_addr;
Addr = & (ipv4-> sin_addr );
 
Inet_ntop (res-> ai_family, addr, ipstr, sizeof (ipstr ));

Printf ("Linked: Host = % s, Port = % d \ n", ipstr, ntohs (ipv4-> sin_port ));
}


RecvSta = recv (sockfd, buffer, sizeof (buffer), 0 );

Printf ("\ n % s \ n", buffer );

Freeaddrinfo (res );
Close (sockfd );

Return 0;
}
# Include <stdio. h>
# Include <string. h>
# Include <sys/types. h>
# Include <sys/socket. h>
# Include <arpa/inet. h>
# Include <netdb. h>

# Define SERVIP "192.168.56.112"
# Define SERVPORT "2349"

Int main (int argc, char * argv [])
{
Struct addrinfo hints, * res;
Int status;
Int sockfd;
Int connSta;
 
Int recvSta;
Char buffer [1024];
Int len;
 
Char ipstr [INET_ADDRSTRLEN];
Void * addr;
 
If (argc! = 3 ){
Fprintf (stderr, "Usage: Not Server IP And Port! ");
Return 1;
}
 
Memset (& hints, 0, sizeof (hints ));
Hints. ai_family = AF_UNSPEC;
Hints. ai_socktype = SOCK_STREAM;
 
Status = getaddrinfo (argv [1], argv [2], & hints, & res );
 
If (status! = 0 ){
Fprintf (stderr, "Error, Server IP And Port! ");
Return 2;
}
 
Sockfd = socket (res-> ai_family, res-> ai_socktype, res-> ai_protocol );
 
If (sockfd = 0 ){
Fprintf (stderr, "Create, socket fail! ");
Return 3;
}
 
ConnSta = connect (sockfd, res-> ai_addr, res-> ai_addrlen );
 
If (connSta! = 0 ){
Fprintf (stderr, "Connect, Remote Host fail! ");
Return 4;
}
Else {
Struct sockaddr_in * ipv4 = (struct sockaddr_in *) res-> ai_addr;
Addr = & (ipv4-> sin_addr );

Inet_ntop (res-> ai_family, addr, ipstr, sizeof (ipstr ));

Printf ("Linked: Host = % s, Port = % d \ n", ipstr, ntohs (ipv4-> sin_port ));
}
 
 
RecvSta = recv (sockfd, buffer, sizeof (buffer), 0 );
 
Printf ("\ n % s \ n", buffer );
 
Freeaddrinfo (res );
Close (sockfd );
 
Return 0;
}

Server Side:

Getaddrinfo ()-> socket ()-> bind ()-> listen ()-> accept ()-> send ()

Code:

# Include <stdio. h>
# Include <string. h>
# Include <sys/types. h>
# Include <sys/socket. h>
# Include <netdb. h>
# Include <arpa/inet. h>
 
# Define SERVPORT "2349"
 
Int main (int argc, char * argv [])
{
Struct addrinfo hints, * res;
Int status;
Int sockfd;

Int connFd;
Struct sockaddr_in cliAddr;

Int sendSta;
Char * msg;

If (argc! = 2 ){
Fprintf (stderr, "Usage: Not found Read File ");
Return 1;
}

Memset (& hints, 0, sizeof (hints ));
Hints. ai_family = AF_UNSPEC;
Hints. ai_socktype = SOCK_STREAM;
Hints. ai_flags = AI_PASSIVE;

Status = getaddrinfo (NULL, SERVPORT, & hints, & res );

If (status! = 0 ){
Fprintf (stderr, "getaddrinfo, fail! ");
Return 2;
}

Sockfd = socket (res-> ai_family, res-> ai_socktype, res-> ai_protocol );

Bind (sockfd, res-> ai_addr, res-> ai_addrlen );

Listen (sockfd, 5 );

Printf ("========= Please Wait Client ===========\ n ");

While (1) {// loop forever!
Char ipstr [INET_ADDRSTRLEN];
Void * addr;

Int len = sizeof (cliAddr );
ConnFd = accept (sockfd, (struct sockaddr *) & cliAddr, & len );

/* View Client IP */
Struct sockaddr_in * ipv4 = (struct sockaddr_in *) & cliAddr;
Addr = & (ipv4-> sin_addr );

Inet_ntop (AF_INET, addr, ipstr, sizeof (ipstr ));
Printf ("client: % s \ n", ipstr );

/* Copy Data */
Msg = "Hello world! ";
SendSta = send (connFd, msg, strlen (msg), 0 );

Close (connFd );
}

Close (sockfd );
Return 0;
}

From the column xiaobin_HLJ80

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.