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...
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...
Assembly Language Programming create an .asm [assembly language program file, using Microsoft Visual Studio or equivalent...
Assembly Language Programming create an .asm [assembly language program file, using Microsoft Visual Studio or equivalent Windows32 integrated development environment (IDE)] program into one that inputs two positive integers, and outputs their greatest common divisor, by implementing the following pseudo-code: input num1 and num2 (assume num1 > 0 and num2 > 0) gcd := num1 remainder := num2 repeat numerator := gcd gcd := remainder remainder := numerator % gcd until (remainder == 0) output gcd The following 3 windows...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT
Active Questions
  • Two flutists blow identical "D" notes of frequency 293.665 Hz on their flutes. Besides having identical...
    asked 1 minute ago
  • Direct Materials Variances The following data relate to the direct materials cost for the production of...
    asked 14 minutes ago
  • If P(A) = 0.4 and P(B) = 0.6, then A and B must be collectively exhaustive....
    asked 19 minutes ago
  • Are monopolies outside of those created through public franchises or patents guilty of monopolization? Does the...
    asked 41 minutes ago
  • Calculate the pH at the equivalence point in titrating 0.100 M solutions of each of the...
    asked 44 minutes ago
  • 1. Construct an explicit bijection between the following sets. Once you've done that, construct injections going...
    asked 1 hour ago
  • What Is Power? The Trial Urban District Assessment (TUDA) measures educational progress within participating large urban...
    asked 2 hours ago
  • Create a Client/Server program in C language
    asked 2 hours ago
  • USING C++ Topics: Friend functions Copy constructor ----------------------------------------------------------------------------------------------------------------------------------------- Lab 3.1 Create your objects in the stack...
    asked 2 hours ago
  • •discuss the following and their uses in computers and engineering: Integrated circuits and use of pins
    asked 3 hours ago
  • Do you believe you should vote for a political candidate based solely on race or gender?...
    asked 3 hours ago
  • Service Department Charges and Activity Bases Harold Corporation, a manufacturer of electronics and communications systems, uses...
    asked 3 hours ago