When writing programs that communicate across a computer
network, one must first invent a protocol, an agreement on how
those programs will communicate. High level decisions must be made
about which program is expected to initiate communication and when
responses are expected.
For example, a Web server is typically thought of as a long-running
program that sends network messages only in response to requests
coming in from the network.
The other side of the protocol is a Web client, such as a browser,
which always initiates communication with the server.
This organization into client and server is used by most network
aware applications.
Deciding that the client always initiates requests tends to
simplify the protocol as well as the programs themselves.
The client application and the server application may be thought of
as communicating via a implementations network protocol, but
actually, multiple layers of network protocols are typically
involved.
Advantages :
1) Complete control over information sent/received on a
network.
2) Best for implementing new protocols for application layer.
3) Writing network monitoring and troubleshooting tools.
4) Used to provide security to higher layer such as SSL/TLS.
5) Best for performance network related applications such as what's
app, games etc.
6) Cloud management.
7) Internet.
Client.c
#include<stdio.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<sys/types.h>
#define RCVBUFSIZE 32 // size of receive buffer
int main(int argc, char*argv[])
{
int sock;
// socket descriptor
struct sockaddr_in echoServAddr; // echo server
address
unsigned short echoServPort; // echo
server port
char* servIP;
// server IP address (dotted quad)
char* echoString;
// string to send to echo server
char echoBuffer[RCVBUFSIZE]; //
buffer for echo string
unsigned int echoStringLen; //
length of string to echo
int bytesRcvd, totalBytesRcvd; //
bytes read in single recv()
// and
total bytes read
if((argc < 3) || (argc > 4)){
fprintf(stderr, "Usage: %s <Server IP>
<Echo Word> [<Echo Port>]\n", argv[0]);
exit(1);
}
servIP = argv[1]; // first arg:
server IP address
echoString = argv[2]; // second arg:
string to echo
if(argc == 4)
echoServPort = atoi(argv[3]); // use
given port, if any
else
echoServPort = 7;
// create a reliable, stream socket using TCP
if((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) <
0){
perror("socket failed");
exit(1);
}
// construct the server address structure
memset(&echoServAddr, 0, sizeof(echoServAddr)); //
zero out structure
echoServAddr.sin_family = AF_INET;
// server address family
echoServAddr.sin_addr.s_addr = inet_addr(servIP); //
server IP address
echoServAddr.sin_port = htons(echoServPort); // server
port
// establish the connection to echo server
if(connect(sock, (struct sockaddr*)&echoServAddr,
sizeof(echoServAddr)) < 0){
perror("connect failed");
exit(1);
}
echoStringLen = strlen(echoString); // determine
input length
// send the string to the server
if(send(sock, echoString, echoStringLen, 0) !=
echoStringLen){
perror("send() sent a different number of bytes
than expected");
exit(1);
}
// receive the same string back from server
totalBytesRcvd = 0;
printf("Received: ");
while(totalBytesRcvd < echoStringLen){
// receive up to the buffer size (minus 1 to
leave space for a null terminator)
// bytes from the sender
if((bytesRcvd = recv(sock, echoBuffer,
RCVBUFSIZE - 1, 0)) <= 0){
perror("recv() failed or connection
closed prematurely");
exit(1);
}
totalBytesRcvd += bytesRcvd;
echoBuffer[bytesRcvd] = '\0';
printf("%s\n", echoBuffer);
}
printf("\n");
close(sock);
return 0;
}
Server.c
#include<stdio.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<sys/types.h>
#define MAXPENDING 5 // maximum connection
requets
#define RCVBUFSIZE 32
void HandleTCPClient(int clntSocket); // TCP client handling function
int main(int argc, char*argv[])
{
int servSock; //
socket descriptor for server
int clntSock; //
socket descriptor for client
struct sockaddr_in echoServAddr; // local address
struct sockaddr_in echoClntAddr; // client
address
unsigned short echoServPort; //
server port
unsigned int clntLen;
// length of client address data structure
if(argc != 2){
fprintf(stderr, "Usage: %s <Server
Port>\n", argv[0]);
exit(1);
}
echoServPort = atoi(argv[1]); // first arg: local port
// create socket for incoming connections
if((servSock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) <
0){
perror("socket() failed");
exit(1);
}
// construct local address structure
memset(&echoServAddr, 0, sizeof(echoServAddr)); //
zero out structure
echoServAddr.sin_family = AF_INET;
// internet address family
echoServAddr.sin_addr.s_addr = htonl(INADDR_ANY); //
any incoming interface
echoServAddr.sin_port = htons(echoServPort); // local
port
// bind to the local address
if(bind(servSock, (struct sockaddr*)&echoServAddr,
sizeof(echoServAddr)) < 0){
perror("bind() failed");
exit(1);
}
// mark the socket so it will listen for incoming
connections
if(listen(servSock, MAXPENDING) < 0){
perror("listen() failed");
exit(1);
}
for(;;) // run forever
{
// set the size of the in-out parameter
clntLen = sizeof(echoClntAddr);
// wait for a client to connect
if((clntSock = accept(servSock, (struct
sockaddr*)&echoClntAddr, &clntLen)) < 0){
perror("accept() failed");
exit(1);
}
printf("Handling client %s\n",
inet_ntoa(echoClntAddr.sin_addr));
HandleTCPClient(clntSock);
}
return 0;
}
void HandleTCPClient(int clntSocket)
{
char echoBuffer[RCVBUFSIZE]; // buffer for echo
string
int recvMsgSize; // size of received
message
// receive message from client
if((recvMsgSize = recv(clntSocket, echoBuffer, RCVBUFSIZE, 0)) <
0){
perror("recv() failed");
exit(1);
}
printf("msg = %s\n", echoBuffer);
// send received string and receive again until end of
transmission
while(recvMsgSize > 0){
// echo message back to client
if(send(clntSocket, echoBuffer, recvMsgSize, 0)
!= recvMsgSize){
perror("send() failed");
exit(1);
}
// see if there is more data to receive
if((recvMsgSize = recv(clntSocket, echoBuffer,
RCVBUFSIZE, 0)) < 0){
perror("recv() failed");
exit(1);
}
}
}
Write technical report about the socket programming which should include the followng: Introduction about the socket...
Using the programming language of your choice, write a client application which: Connects to a network service, then Asks for version information, then Receives the response, then Disconnects and Prints out the response. Detail: The program will take no input from the user. When your program is executed, it should connect to IP address 64.183.98.170 on port 3800. Upon successful connection, send the string: version\n where \n is an end of line marker. The server will respond with a string...
implement the follwing code using command promp or Eclipse or any other program you are familiar with. keep the name of the classes exatcly the same as requested for testing purpose. please follow each instruction provided below Objective of this assignment o get you famililar with developing and implementing TCP or UDP sockets. What you need to do: I. Implement a simple TCP Client-Server application 2. and analyze round trip time measurements for each of the above applications 3. The...
You are to write a Java program using the following instructions to build a bank-ATM server system with sockets. A bank holds accounts that can have deposits, withdrawals, or retrievals of balance. The bank server provides the account for transactions when a client uses and ATM machine. The ATM asks for the type of transaction and the amount (if needed), then creates a socket connection to the bank to send the transaction for processing. The bank performs the transaction on...
Using java socket programming rewrite the following program to handle multiple clients simultaneously (multi threaded programming) import java.io.*; import java.net.*; public class WelcomeClient { public static void main(String[] args) throws IOException { if (args.length != 2) { System.err.println( "Usage: java EchoClient <host name> <port number>"); System.exit(1); } String hostName = args[0]; int portNumber = Integer.parseInt(args[1]); try ( Socket kkSocket = new Socket(hostName, portNumber); PrintWriter out = new PrintWriter(kkSocket.getOutputStream(), true); BufferedReader in = new BufferedReader( new InputStreamReader(kkSocket.getInputStream())); ) { BufferedReader...
This is in C. For this assignment we will write a simple database server. We will be creating a simple database of student records, so let’s describe these first. The format of a student record is as follows: typedef struct student { char lname[ 10 ], initial, fname[ 10 ]; unsigned long SID; float GPA; } SREC; Part One – the Server We will create a database server. The job of the server is to accept a...
C Programming - Please Help us! Implementing Load Balancing, the 3 Base Code files are at the bottom: Implementing Load Balancing Summary: In this homework, you will be implementing the main muti-threaded logic for doing batch based server load balancing using mutexes Background In this assignment you will write a batch-based load balancer. Consider a server which handles data proces- sing based on user requests. In general, a server has only a fixed set of hardware resources that it can...
can i get some help with this program CMPS 12B Introduction to Data Structures Programming Assignment 2 In this project, you will write a Java program that uses recursion to find all solutions to the n-Queens problem, for 1 Sns 15. (Students who took CMPS 12A from me worked on an iterative, non-recursive approach to this same problem. You can see it at https://classes.soe.ucsc.edu/cmps012a/Spring l8/pa5.pdf.) Begin by reading the Wikipcdia article on the Eight Queens puzzle at: http://en.wikipedia.org/wiki/Eight queens_puzzle In...
And there was a buy-sell arrangement which laid out the conditions under which either shareholder could buy out the other. Paul knew that this offer would strengthen his financial picture…but did he really want a partner?It was going to be a long night. read the case study above and answer this question what would you do if you were Paul with regards to financing, and why? ntroductloh Paul McTaggart sat at his desk. Behind him, the computer screen flickered with...
How can we assess whether a project is a success or a failure? This case presents two phases of a large business transformation project involving the implementation of an ERP system with the aim of creating an integrated company. The case illustrates some of the challenges associated with integration. It also presents the obstacles facing companies that undertake projects involving large information technology projects. Bombardier and Its Environment Joseph-Armand Bombardier was 15 years old when he built his first snowmobile...