Self-measured socket. Study Notes

Source: Internet
Author: User
Tags socket port number htons

Measured for a long time, finally measured through, a total of two cases.

The first case: send yourself to yourself. Configuration: Client and server IP are set to the same IP, I am using 127.0.0.1 during the testing process. The rest remains the same.

The second scenario: Place the client and server in two virtual machines, respectively. Here about the settings in the virtual machine to kill the amount, perhaps their own about the virtual machine is not familiar with, so on the virtual machine settings, can ping between each other, and the outside machine can also ping, I boil a night, finally is straightened out.

In their own words summed up: the outside of the machine and virtual machine Vmnet8,vmnet1 are in a network segment, two virtual machine IP also and their three in a network segment, testing, and finally they can ping each other.

Here is a critical issue is to be sure to put the virtual machine in the firewall shut down, in fact, the firewall can be found, and I am looking for a virtual machine firewall but it took a long time, and finally have to use the command, about shutting down the virtual machine firewall commands, in my blog mentioned, there is a need to look for.

Because I am not very familiar with the socket, some code is not understood, but I test pass is used to write the IP to die. The function used is inet_addr (), when the header file <arpa/inet.h>

The code is as follows:

Service side:

/*
* SEVER.C
*
* Created On:aug 23, 2012
* AUTHOR:CYF
*/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <netinet/in.h>
#include <arpa/inet.h>//add by Cuiyifang to state Inet_ntoa.
#include <sys/socket.h>
#include <sys/wait.h>
#define SERVPORT 3649/* Server listening port number */
#define BACKLOG 10/* Maximum simultaneous connection requests */
int main ()
{
int sockfd,client_fd,sin_size; /*SOCK_FD: Monitor SOCKET;CLIENT_FD: Data transfer Socket */
struct sockaddr_in my_addr; /* Native Address information */
struct sockaddr_in remote_addr; /* Client Address information */
fprintf (stderr, "remote_addr.sin_port=%d\n", Remote_addr.sin_port);
SOCKFD = socket (af_inet, sock_stream, 0);
fprintf (stderr, "sockfd=%d\n", SOCKFD);
if ((SOCKFD = socket (af_inet, sock_stream, 0)) = = = 1)
if (SOCKFD = =-1)
{
Perror ("Socket creation error. ");
Exit (1);
}
fprintf (stderr, "remote_addr.sin_port=%d\n", Remote_addr.sin_port);
My_addr.sin_family=af_inet;
My_addr.sin_port=htons (Servport);
My_addr.sin_addr.s_addr =inaddr_any;//inet_addr ("127.0.0.1");//
Bzero (& (My_addr.sin_zero), 8);
fprintf (stderr, "remote_addr.sin_port=%d\n", Remote_addr.sin_port);
if (Bind (SOCKFD, (struct sockaddr *) &my_addr, sizeof (struct sockaddr)) = =-1)
{
Perror ("Bind error. ");
Exit (1);
}
fprintf (stderr, "bind success!");
if (Listen (SOCKFD, BACKLOG) = =-1)
{
Perror ("Listen error. ");
Exit (1);
}
fprintf (stderr, "Listen success!");
while (1)
{
sin_size = sizeof (struct sockaddr_in);
if (client_fd = Accept (SOCKFD, (struct sockaddr *) &remote_addr, &sin_size)) = =-1)
{
Perror ("Accept error");
Continue
}
fprintf (stderr, "accept success!");
printf ("received a connection from%s", Inet_ntoa (REMOTE_ADDR.SIN_ADDR));
if (!fork ())
{/* Child Process Code Snippet */
if (Send (CLIENT_FD, "Hello, connected!", 26, 0) = =-1)
Perror ("Send error. ");
Close (CLIENT_FD);
Exit (0);
}
Close (CLIENT_FD);
}
return 0;
}


Client

/*
* CLIENT.C
*
* Created On:aug 22, 2012
* Author:sean
*/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <unistd.h>//add by Cuiyifang to state function close ().
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#define Servport 3649
#define MAXDATASIZE 100//Maximum data transfer per time */
#define HOST 192.168.4.87
int main (int argc, char *argv[])
{
int SOCKFD, recvbytes;
Char Buf[maxdatasize];
struct sockaddr_in my_addr;
struct Hostent *host;
struct sockaddr_in serv_addr;
struct SERV_ADDR;
/*if (ARGC < 2)
{
fprintf (stderr, "Please enter the server ' s host name! ");
}
Else
Exit (1); */
Host=gethostbyname (argv[1]);
/*host=gethostbyname (argv[1]);
fprintf (stderr, "%s", argv[1]); */
/*if ((Host=gethostbyname (argv[1)) ==null)
{
Herror ("Get Host by name" error.) ");
Exit (1);
}
Else
fprintf (stderr, "GetName success!"); */
if ((SOCKFD = socket (af_inet, sock_stream, 0)) = = = 1)
{
Perror ("Socket creation error. ");
Exit (1);
}
fprintf (stderr, "Socket build success!");

My_addr.sin_family=af_inet;
my_addr.sin_port=0;
MY_ADDR.SIN_ADDR.S_ADDR = Inaddr_any;
if (Bind (SOCKFD, (struct sockaddr *) &my_addr, sizeof (struct sockaddr_in)) = =-1)
{
Perror ("Bind error. ");
Exit (1);
}
fprintf (stderr, "bind sucess!");
Serv_addr.sin_family=af_inet;
Serv_addr.sin_port=htons (Servport);
SERV_ADDR.SIN_ADDR.S_ADDR = inet_addr ("127.0.0.1"); (struct sockaddr *) &serv_addr
SERV_ADDR.SIN_ADDR.S_ADDR=INET_ADDR ("192.168.4.129");
SERV_ADDR.SIN_ADDR = h_addr;
SERV_ADDR.SIN_ADDR = * (host);
Bzero (& (Serv_addr.sin_zero), 8);
fprintf (stderr, "bind sucess!");
if (Connect (SOCKFD, (struct sockaddr *) &serv_addr, sizeof (struct sockaddr_in)) = =-1)
{
Perror ("Connect error. ");
Exit (1);
}
fprintf (stderr, "connet success!");
if ((Recvbytes=recv (SOCKFD, buf, maxdatasize, 0)) ==-1)
{
Perror ("recv error. ");
Exit (1);
}
fprintf (stderr, "recv success!");
Buf[recvbytes] = "n";
Buf[recvbytes] = 0;
printf ("Received:%s", buf);
fprintf (stderr, "Received:%s", buf);
Close (SOCKFD);
return 0;
}

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.