It's not a good idea multiple server talking to single client, and it doesn't serve the purpose. It's suffice to get single server to serve the purpose for a single client, but multiple clients can talk to single server.
TCP Server
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <errno.h>
#include <signal.h>
#include <unistd.h>
#include <string.h>
#include <arpa/inet.h>
#include <sys/wait.h>
#define SERV_PORT 49999
#define BACKLOG 16
int main (void)
{
// Declare and define
int ls = 0; // Listen socket descriptor (reference)
int s = 0; // socket descriptor (reference)
char buffer[256] = {'\0'}; // Data buffer
char *ptr = buffer; // Data buffer
int len = 0; // Number of bytes to send or receive
int maxLen = sizeof(buffer); // Maximum number of bytes to receive
int n = 0; // Number of bytes for each recv call
struct sockaddr_in servAddr; // !ADDED!
struct sockaddr_in clntAddr; // !ADD!
socklen_t clntAddrLen = sizeof(clntAddr); // !ADD!
// Create listen socketd
ls = socket(AF_INET, SOCK_STREAM, 0);
if (ls < 0) {
perror("Error: Listen socket failed!");
exit(1);
}
// Create local (server) socket address
memset(&servAddr, '0', sizeof(servAddr));
servAddr.sin_family = AF_INET;
servAddr.sin_addr.s_addr = INADDR_ANY; // Default IP address
servAddr.sin_port = htons(SERV_PORT); // Default port
memset(&(servAddr.sin_zero ), '\0', 8);
// Bind listen socket to the local socket address
if (bind (ls, (struct sockaddr *)&servAddr, sizeof(servAddr)) < 0) {
perror("Error: binding failed!");
exit(1);
}
// Listen to connection requests
if (listen(ls, BACKLOG) < 0) {
perror("Error: listening failed!");
exit(1);
}
printf("Server sockfd %d backlog %d Server's port %d \n", ls, BACKLOG, servAddr.sin_port);
// Handle the connection
for ( ; ; ) { // Run Forever
printf("for ( ; ; )\n");
// Accept connections from client
if ((s = accept (ls, (struct sockaddr *)&clntAddr, &clntAddrLen)) < 0) {
//perror ("Error: accepting failed!);
perror ("Error: accepting failed!");
exit (1);
}
printf("Server : sockfd %d accept sockfd %d client's port %d\n", ls, s, clntAddr.sin_port);
// Data transfer section
if (( n = recv( s, buffer, sizeof(buffer), 0 ) ) <= 0) {
perror("recv");
} else {
printf("Recv %d \n", n);
}
int dataSend=0;
dataSend = send(s, buffer, n, 0); // Send back (echo) all bytes received
if (dataSend==-1) {
printf("dataSend==-1 \n");
perror("send");
} else {
printf("Send %d byte \n", dataSend);
}
// Close the socket
close (s);
} // End of for loop
} // End of echo server program
|
1
2
3
4
5
6
7
8
9
10
11
12
13
| Server sockfd 3 backlog 16 Server's port 20419
for ( ; ; )
Server : sockfd 3 accept sockfd 4 client's port 49325
Recv 2
Send 2 byte
for ( ; ; )
Server : sockfd 3 accept sockfd 4 client's port 49325
Recv 5
Send 5 byte
for ( ; ; )
Server : sockfd 3 accept sockfd 4 client's port 49325
Recv 3
Send 3 byte
|
TCP Client
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <errno.h>
#include <signal.h>
#include <unistd.h>
#include <string.h>
#include <arpa/inet.h>
#include <sys/wait.h>
int main (int argc, char* argv[ ]) // Three arguments to be checked later
{
// Declare and define
int s = 0; // Socket descriptor
int n = 0; // Number of bytes in each recv call
char *servName = NULL; // Server name
int servPort = 0; // Server port number
char *string = NULL; // String to be echoed
char buffer[256 + 1] = {'\0'};; // Buffer
char *ptr = buffer; // Pointer to move along the buffer
struct sockaddr_in servAddr; // Server socket address
int dataSend = 0;
// Check and set arguments
if (argc != 4) {
printf("Error: three arguments are needed!");
exit(1);
}
servName = argv[1];
servPort = atoi(argv[2]);
string = argv[3];
// Create socket
if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
printf("\n Error : Could not create socket \n");
return 1;
}
// Create remote (server) socket address
memset(&servAddr, '0', sizeof(servAddr));
servAddr.sin_family = AF_INET;
servAddr.sin_port = htons(servPort);
if (inet_pton(AF_INET, argv[1], &servAddr.sin_addr) <= 0) {
printf("\n inet_pton error occured\n");
return 1;
}
// Connect to the server
if (connect(s, (struct sockaddr *)&servAddr, sizeof(servAddr)) < 0) {
perror("Error: connection failed!");
exit(1);
}
printf("Client : connected to the server with socket fd %d Server's port %d\n", s, servAddr.sin_port);
// Data transfer section
if ((dataSend = send(s, string, strlen(string), 0)) == -1) {
printf("dataSend==-1 \n");
perror("send");
} else {
printf("else \n");
}
if ((n = recv( s, buffer, sizeof(buffer), 0)) <= 0 ) {
perror("recv");
} else {
printf("Recv %d byte \n", n);
}
// Print and verify the echoed string
buffer[n] = '\0';
printf("Echoed string received: \n");
fputs(buffer, stdout);
printf("\n");
// Close socket
close(s);
// Stop program
exit(0);
} // iEnd of echo client progra[
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
| shirley:~$ ./tcpc 0.0.0.0 49999 HI
Client : connected to the server with socket fd 3 Server's port 20419
else
Recv 2 byte
Echoed string received:
HI
shirley:~$ ./tcpc 0.0.0.0 49999 Hello
Client : connected to the server with socket fd 3 Server's port 20419
else
Recv 5 byte
Echoed string received:
Hello
shirley:~$ ./tcpc 0.0.0.0 49999 Bye
Client : connected to the server with socket fd 3 Server's port 20419
else
Recv 5 byte
Echoed string received:
Bye
|
Comments
Post a Comment