The socket code written by Jia dawang is quite good and well-regulated, but it seems I still need to change it. Add it to favorites first.
Client
# Include <unistd. h>
# Include <stdio. h>
# Include <stdlib. h>
# Include <string. h>
# Include <errno. h>
# Include <sys/types. h>
# Include <arpa/inet. h>
# Include <sys/socket. h>
# Include <arpa/inet. h>
# Include <netinet/in. h>
/**
* Title: It seems to have been a previous exercise on the computer.
* Author: Eric Jia
* Desc: Network Programming seems to send a message to the other party to receive and chat with each other.
* Add: This is the client. To run it, run the server first, and then run the client. When the client is running, add the Server ip address as the parameter.
* For example, if the local communication occurs, run the client:./client 127.0.0.1 after running the server.
* Then the client and the server can talk about high ..
*/
# Define SEND_MAX 256
# Define PORT 7227
Void recv_try (int sockfd ){
Int n;
Char buf [SEND_MAX];
While (n = recv (sockfd, buf, SEND_MAX, 0)> 0 ){
Printf ("** Others Said: ** \ r \ n ");
Printf ("% s \ r \ n", buf );
Printf ("******* \ n ");
}
If (n <0)
Printf ("** Recive Error ** \ n ");
Else
Printf ("** Seems Server Has Disconnected Connection ** \ n ");
}
Void err_exit (char * msg ){
Printf ("% s", msg );
Exit (1 );
Return;
}
Void err (char * msg ){
Printf ("% s", msg );
Return;
}
Int main (int argc, char * argv []) {
Int sock;
Struct sockaddr_in sin;
If (argc! = 2 ){
Err_exit ("usage: [ProgramName] [HostIP] \ r \ n ");
} Else if (sock = socket (AF_INET, SOCK_STREAM, 0) <0 ){
Err_exit ("** Seems a Error Caused In Getting a Socket ** \ r \ n ");
}
Sin. sin_family = AF_INET;
Sin. sin_addr.s_addr = inet_addr (argv [1]);
Sin. sin_port = PORT;
If (connect (sock, (struct sockaddr *) & sin, sizeof (sin) <0)
{
Err_exit ("** Seems the Server is Not Ready ** \ r \ n ");
} Else {
Printf ("** Successful Connected! ; D ** \ n ");
}
Pid_t pid;
If (pid = fork () <0)
Err_exit ("Fork Error \ r \ n ");
Else if (pid = 0 ){
Recv_try (sock );
} Else {
While (1 ){
Char buf [SEND_MAX];
Memset (buf, 0, SEND_MAX );
Gets (buf );
If (! Strcmp (buf, ": cmd_exit ")){
Close (sock );
Break;
}
Write (sock, buf, SEND_MAX );
Printf ("\ n ");
}
}
Return 0;
}
Server:
# Include <unistd. h>
# Include <stdio. h>
# Include <stdlib. h>
# Include <string. h>
# Include <errno. h>
# Include <sys/types. h>
# Include <arpa/inet. h>
# Include <sys/socket. h>
# Include <arpa/inet. h>
# Include <netinet/in. h>
/**
* Title: It seems to have been a previous exercise on the computer.
* Author: Eric Jia
* Desc: Network Programming seems to send a message to the other party to receive and chat with each other.
* Add: This is the client. To run it, run the server first, and then run the client. When the client is running, add the Server ip address as the parameter.
* For example, if the local communication occurs, run the client:./client 127.0.0.1 after running the server.
* Then the client and the server can talk about high ..
*/
# Define SEND_MAX 256
# Define PORT 7227
# Define MAX_CONNECTION 10
Void err_exit (char * msg ){
Printf ("% s", msg );
Exit (1 );
Return;
}
Void err (char * msg ){
Printf ("% s", msg );
Return;
}
Void recv_try (int sockfd ){
Int n;
Char buf [SEND_MAX];
While (n = recv (sockfd, buf, SEND_MAX, 0)> 0 ){
Printf ("** Others Said: ** \ r \ n ");
Printf ("% s \ r \ n", buf );
Printf ("******* \ n ");
}
If (n <0)
Printf ("** Recive Error ** \ n ");
Else
Printf ("** Seems Client Has Disconnected Connection ** \ n ");
}
Int main (int argc, char * argv []) {
Int sock;
Struct sockaddr_in sin;
If (sock = socket (AF_INET, SOCK_STREAM, 0) <0 ){
Err_exit ("** Seems a Error Caused In Getting a Socket ** \ n ");
}
Sin. sin_family = AF_INET;
Sin. sin_addr.s_addr = inet_addr ("127.0.0.1 ");
Sin. sin_port = PORT;
If (bind (sock, (struct sockaddr *) & sin, sizeof (sin )))
Err_exit ("Err Bind \ n ");
Else if (listen (sock, MAX_CONNECTION ))
Err_exit ("ERR Listen \ n ");
Struct sockaddr_in client;
Int sin_size = sizeof (client );
Int new_sock = accept (sock, (struct sockaddr *) & client, (socklen_t *) & sin_size );
Pid_t pid;
If (pid = fork () <0)
Err_exit ("Fork Error \ n ");
Else if (pid = 0 ){
Recv_try (new_sock );
} Else {
While (1 ){
Char buf [SEND_MAX];
Memset (buf, 0, SEND_MAX );
Gets (buf );
If (! Strcmp (buf, ": cmd_exit ")){
Close (new_sock );
Break;
}
Write (new_sock, buf, SEND_MAX );
Printf ("\ n ");
}
}
Return 0;
}
I have time to change it after I finish the test tomorrow.