Home > other >  Network programming problems, "TCP/IP network programming" first chapter is the first exam
Network programming problems, "TCP/IP network programming" first chapter is the first exam

Time:10-05

Problem description: according to "TCP/IP network programming" the first chapter gives an example of the
In a computer inside a VMware virtual machine installed Ubuntu programs are as follows:
Hello_server. C
 # include & lt; stdio.h> 
#include
#include
#include
#include
#include

Void error_handling (char * message);

Int main (int arg c, char * argv [])
{
Int serv_sock;
Int clnt_sock;

Struct sockaddr_in serv_addr.
Struct sockaddr_in clnt_addr;
Socklen_t clnt_addr_size;

Char message []="Hello World!" ;

If (arg c!=2) {
Usage: printf (" % s & lt; Port> \ n ", argv [0]);
exit(1);
}

Serv_sock=socket (PF_INET SOCK_STREAM, 0).
If (serv_sock==1)
Error_handling (" socket () error ");

Memset (& amp; Serv_addr, 0, sizeof (serv_addr));
Serv_addr. Sin_family=AF_INET;
Serv_addr. The sin_addr. S_addr=htonl (INADDR_ANY);
Serv_addr. Sin_port=htons (atoi (argv [1]));

If (bind (serv_sock, (struct sockaddr *) & amp; Serv_addr, sizeof (serv_addr))==1)
Error_handling (" bind () error ");

If (listen (serv_sock, 5)==1)
Error_handling (" listen () error ");

Clnt_addr_size=sizeof (clnt_addr);
Clnt_sock=accept (serv_sock, (struct sockaddr *) & amp; Clnt_addr, & amp; Clnt_addr_size);
If (clnt_sock==1)
Error_handling (" the accept () error ");

Write (clnt_sock, message, sizeof (the message));
Close (clnt_sock);
Close (serv_sock);
return 0;
}

Void error_handling (char * message)
{
The fputs (message, stderr);
Fputc (' \ n 'stderr);
exit(1);
}

Then compile
 GCC hello_server. C - o hserver 

Start the server program
./hserver 9190 

Now in a computer to write the client program
Hello_client. C
 # include & lt; stdio.h> 
#include
#include
#include
#include
#include

Void error_handling (char * message);

Int main (int arg c, char * argv [])
{
Int the sock;
Struct sockaddr_in serv_addr.
Char message [30].
Int str_len;

If (arg c! {
=3)Usage: printf (" % s & lt; IP> & lt; Port> \ n ", argv [0]);
exit(1);
}

The sock=socket (PF_INET SOCK_STREAM, 0).
If (the sock==1)
Error_handling (" socket () error ");

Memset (& amp; Serv_addr, 0, sizeof (serv_addr));
Serv_addr. Sin_family=AF_INET;
Serv_addr. The sin_addr. S_addr=inet_addr (argv [1]).
Serv_addr. Sin_port=htons (atoi (argv [2]));

If (connect (the sock, (struct sockaddr *) & amp; Serv_addr, sizeof (serv_addr))==1)
Error_handling (" the connect () error!" );

Str_len=read (the sock, message, sizeof (message) - 1);
If (str_len==1)
Error_handling (" the read () error!" );

Printf (" the Message from the server: % s \ n ", Message);
Close (sock);
return 0;
}

Void error_handling (char * message)
{
The fputs (message, stderr);
Fputc (' \ n 'stderr);
exit(1);
}

Compiler
 GCC hello_client. C - o hclient 

Run the client program
./hclient computer 1 IP address 9190 

Could not connect the server program, under the same computer can run successfully, you the problem of the virtual machine network configuration
  • Related