After reading the Linux programming about socket socket This chapter of the communication flow of the introduction, wrote the client and server side of the program. There are some problems that need attention.
Client:
-
Socket Internet address S_ADDR cannot be used to assign values directly with 127.0.0.1 or the string "127.0.0.1", but instead should use INET_ADDR ("127.0.0.1").
-
In the Connect function, Address_len should not write numbers directly, for example, 4, but should use sizeof (address), where addresses are sockets, struct sockaddr_in types.
-
The address in the Connect function cannot be directly addressed with the SOCKADDR_IN type variable, but should be added to this address (struct sockaddr*) to force type conversion
-
The three arguments in the write function, not just a string. The three parameters are the client socket, the second is the cached address of the data to be sent, and the subsequent data length, directly with the number.
-
There should be a parameter in close () for the socket.
-
The third parameter in the socket function should be written with a default of 0.
-
The socket function returns a descriptor of type int, and it is best not to use it to determine whether the socket was created successfully.
#include <sys/socket.h> #include <netinet/in.h> #include <stdio.h> //the following three wardrobe files are not introduced # include<sys/types.h> #include <arpa/inet.h> #include <unistd.h>int main () { //char *buff; int serversoc=socket (af_inet,sock_stream,0), clientsoc; struct sockaddr_in address,clientaddress; address.sin_family=AF_INET; address.sin_port=9734; &NBSP;ADDRESS.SIN_ADDR.S_ADDR=INET_ADDR ("127.0.0.1"); if (Bind (SERVERSOC, (struct sockaddr*) & Address,sizeof (address))) { printf ("bind fail!"); return -1; } if (Listen (serversoc,8)) { printf (" Listen fail! "); return -1; } size_t client_addr_len=sizeof (address); int len=sizeof ( clientaddress); while (1) { char ch; clientsoc=accept (SERVERSOC, (struct sockaddr* ) &clientaddress, (socklen_t *__restrict) &len) read (clientsoc,&ch,1); printf (" data from client:%c\n ", ch); //ch++; //write ( clientsoc,&ch,1); close (CLIENTSOC); } return 0;}
Server-side:
-
The first parameter in the read () function is the client's descriptor, not the server-side descriptor.
-
The third parameter of the Accept () function should be the client's address length pointer, cannot use &SIZEOF (address) as the argument, sizeof returns the object, etc., should use int len=sizeof (address); Then the argument &len. And before &len, add (socklen_t *__restrict) to cast.
-
The second parameter of the Accept () function is the address pointer of the client, not the server's address pointer. And you want to convert the type.
-
The descriptor in close () is the client descriptor returned by accept, not the server descriptor.
-
The Accept () and read () should be placed in the while. Loop Receive and read
#include <sys/socket.h> #include <netinet/in.h> #include < stdio.h> //the following three wardrobe files do not introduce #include<sys/types.h> #include <arpa/inet.h> #include < Unistd.h>int main () { char ch= ' a '; int clientsoc=socket (AF_ inet,sock_stream,0); struct sockaddr_in address; address.sin_ Family=af_inet; address.sin_port=9734; address.sin_addr.s_addr=inet_addr (" 127.0.0.1 "); int connec=connect (Clientsoc, (struct sockaddr*) &address,sizeof ( Address), if (Connec) {printf ("connection fail"); return -1;} write (clientsoc,&ch,1); //read (clientsoc,&ch,1); / /printf ("data from server:%c\n", ch); close (CLIENTSOC); return 0;}
If it is not on the same machine, modify the IP and so on.
Note: I found a particularly funny but also particularly worth thinking about the problem-in the server side of the output can not be omitted, otherwise cannot see the data.
Another interesting thing is that after the char type is replaced with char*, the corresponding output is modified, there is no difference between the and no, at this point will be \ nthe space and no data displayed on the server. Why is it? Worth pondering!
c/S program under Linux