Question

Create a Client/Server program in C language

Create a Client/Server program in C language

Homework Answers

Answer #1

SERVER CODE

#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 of socket are: ----*/

/* 1) Internet domain 2) Stream socket 3) Default protocol */

welcomeSocket = socket(PF_INET, SOCK_STREAM, 0);

/*---- Configure the settings of 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("Listeningn");

else

    printf("Errorn");

/*---- 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 Worldn");

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;

}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Please use Python programming language to write a client program and a server program, so that...
Please use Python programming language to write a client program and a server program, so that these two programs communicate through a TCP connection based on the following scenario: Both of the client program and the server program are running on the same localhost. The client program prompts the user to enter the radius of a circle and pass it to the server program. The server program computes the area of this circle and passes it to the client program....
Create a program in C++ language, create a flowchart, provide a screenshot of the program and...
Create a program in C++ language, create a flowchart, provide a screenshot of the program and output, and involve looping for your program: Nested loops: Challenge. Write a program that calculates and displays the yearly amount available if $1000 is invested in a bank account for 10 years. Your program should display the amounts available for interest rates from 6% to 12% inclusively, in 1% increments. Use a nested loop, with the outer loop controlling the interest rate and the...
Client / Server using named pipes with thread and fork() in C/C++ **Note** You will need...
Client / Server using named pipes with thread and fork() in C/C++ **Note** You will need to write a client program and a server program for this question to be correct. ** **Cannot use socket** client the client application that will get the command from the user and pass the command to be executed from the command line, parses the command and puts a binary representation of the parse into a shared memory segment. At this point, the client will...
Create a very simple TCP Application in JAVA. You will create a client and a server....
Create a very simple TCP Application in JAVA. You will create a client and a server. The client must execute after you start the server. Here is what they will do. The client will read from the console a string that the users enters. This string will be the name of the user, let's say Bob. The client then sends the name, Bob, in an Object Stream, to the server. The server will receive this Object Stream and send to...
Please perform the code in the ubuntu terminal: Create and configure a Client / Server architecture...
Please perform the code in the ubuntu terminal: Create and configure a Client / Server architecture that allows the following operations with files from / to the Client: Copy, move, delete Chat between users that allows the exchange of files, including videos
Operating Systems For Linux/Ubunbtu, create and configure a client / server architecture that allows the following...
Operating Systems For Linux/Ubunbtu, create and configure a client / server architecture that allows the following operations with files from / to the client: -Copy, move, delete. -Chat between users that allows the exchange of files, including videos.
It is relatively easy to write a Web server in any language. The use of different...
It is relatively easy to write a Web server in any language. The use of different languages on the client side is more constrained why is this?
1. Please use only the C as the language of programming. 2. Please submit/upload on Canvas,...
1. Please use only the C as the language of programming. 2. Please submit/upload on Canvas, the following les for each of your programs: (1) the client and the server source les each (2) the client and the serve executable les each (3) a brief Readme le that shows the usage of the program. 3. Please appropriately comment your program and name all the identiers suitable, to enable enhanced readability of the code. Problems 1. Write an ftp client that...
Hello. for one of my assignment, I need to create any program using the C language...
Hello. for one of my assignment, I need to create any program using the C language and that must contain the following: - Error Checking - Use of Functions - Menu system - Array Processing It can be about anything, nothing specified but must contain the features mentioned above Thank you
Write a simple TCP program for a client that reads a line of text from its...
Write a simple TCP program for a client that reads a line of text from its standard input (keyboard) and sends the line out its socket to the server. The server reads the line from the socket and prints it in its standard output (screen). The server then reads a line from its standard input (keyboard) and sends it out its socket to the client. When the client sends “!” the server should reply back “bye” to the client and...