I am trying to create a three simple programs (two in C, one in Java) that will
ID: 3662272 • Letter: I
Question
I am trying to create a three simple programs (two in C, one in Java) that will communicate over a network (a simple 'Hello world' message): a tcp-client in C, a tcp-client in java, and tcp-server in C. The programs must compile in a UNIX-based environment and execute system calls such as (but not limited to) socket(), bind(), gethostbyname(), gethostname(), read(), write(), send(), listen(), pthread_create(), pthread_detach(), getsockname(), and pthread_exit(), etc. For this inquiry, I am just interested in the server program although help with the client porgrams would be great as well. The server program must be able to accept multiple requests from clients and respond simultaneously. The server program (in C) will take a port number as an argument on the command line, used to bind the socket and recieve messages on the correct port; it will also create a server socket (not using auto-binding but the provided port number) for binding the socket. It then will wait for incoming requests from client programs. When a request is recieved, it should create a new detacthed thread used for interpreting the mesage by parsing. The messages recieved will be like html tags. For instance, if <ping>Hello</ping> is recieved, it will respond with the same message (only, say using the tag <reply>Hello</reply>). Other tags ultimately will be used and implemented but I am only interested in just getting the simple message echo to work. The child thread will then close the socket and terminate the connection. The main thread will continue to listen. Any implementation suggestions or help would be greatly appreciated.
Explanation / Answer
Can help u with this any help feel free to comment:
ServerCode:
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
int main()
{
int welcomeSocket, newSocket;
char buffer[1024];
struct sockaddr_in serverAddr;
struct sockaddr_storage serverStorage;
socklen_t addr_size;
/*---- Create the socket. The three arguments are: ----*/
/* 1) Internet domain 2) Stream socket 3) Default protocol (TCP in this case) */
welcomeSocket = socket(PF_INET, SOCK_STREAM, 0);
/*---- Configure settings of the server address struct ----*/
/* Address family = Internet */
serverAddr.sin_family = AF_INET;
/* Set port number, using htons function to use proper byte order */
serverAddr.sin_port = htons(7891);
/* Set IP address to localhost */
serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
/* Set all bits of the padding field to 0 */
memset(serverAddr.sin_zero, '', sizeof serverAddr.sin_zero);
/*---- Bind the address struct to the socket ----*/
bind(welcomeSocket, (struct sockaddr *) &serverAddr, sizeof(serverAddr));
/*---- Listen on the socket, with 5 max connection requests queued ----*/
if(listen(welcomeSocket,5)==0)
printf("Listening ");
else
printf("Error ");
/*---- Accept call creates a new socket for the incoming connection ----*/
addr_size = sizeof serverStorage;
newSocket = accept(welcomeSocket, (struct sockaddr *) &serverStorage, &addr_size);
/*---- Send message to the socket of the incoming connection ----*/
strcpy(buffer,"Hello World ");
send(newSocket,buffer,13,0);
return 0;
}
Client code:
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
int main()
{
int clientSocket;
char buffer[1024];
struct sockaddr_in serverAddr;
socklen_t addr_size;
/*---- Create the socket. The three arguments are: ----*/
/* 1) Internet domain 2) Stream socket 3) Default protocol (TCP in this case) */
clientSocket = socket(PF_INET, SOCK_STREAM, 0);
/*---- Configure settings of the server address struct ----*/
/* Address family = Internet */
serverAddr.sin_family = AF_INET;
/* Set port number, using htons function to use proper byte order */
serverAddr.sin_port = htons(7891);
/* Set IP address to localhost */
serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
/* Set all bits of the padding field to 0 */
memset(serverAddr.sin_zero, '', sizeof serverAddr.sin_zero);
/*---- Connect the socket to the server using the address struct ----*/
addr_size = sizeof serverAddr;
connect(clientSocket, (struct sockaddr *) &serverAddr, addr_size);
/*---- Read the message from the server into the buffer ----*/
recv(clientSocket, buffer, 1024, 0);
/*---- Print the received message ----*/
printf("Data received: %s",buffer);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.