Question

In: Computer Science

Socket-based Java server program

Write a Socket-based Java server program that responds to client messages as follows: When it receives a message from a client, it simply converts all the letters into ASCII characters and sends back them to the client. Write both client and server programs demonstrating this.

Solutions

Expert Solution

Server Program:

import java.io.*;
import java.net.*;

public class Server
{
public static void main(String args[])
{
String inputMessage;
String outputMessage;

try {
ServerSocket ssock = new ServerSocket(6789);
while(true)
{
Socket connsock = ssock.accept();
InputStreamReader inStr =  new InputStreamReader(connsock.getInputStream());
BufferedReader inNet = new BufferedReader(inStr);
DataOutputStream outNet = new DataOutputStream(connsock.getOutputStream());
inputMessage = inNet.readLine();
outputMessage = inputMessage.toUpperCase();
outNet.writeBytes(outputMessage);
}
}
catch(IOException e) {
System.out.println(e.getMessage());
}
}
}
Client Program

import java.io.*;
import java.net.*;

public class Client  
{
public static void main(String[] args)
{
String  message = null;
try {
BufferedReader kbd = new BufferedReader(new InputStreamReader(System.in));
Socket csock = new Socket("localhost", 6789);
DataOutputStream outNet = new DataOutputStream(csock.getOutputStream());
BufferedReader inNet = new BufferedReader(new InputStreamReader(csock.getInputStream()));
message = kbd.readLine();
outNet.writeBytes(message + "");
message = inNet.readLine();
csock.close();
System.out.println("Server sent: " + message);
}
catch(IOException e) {
System.out.println(e.getMessage());
}
}
}
Output:
First, run the server using the java Server command and then open the second window and run the client.

C:> java Client
How Are You
Server sent: HOW ARE YOU


Program with following output was created



C:> java Client
How Are You
Server sent: HOW ARE YOU

Related Solutions

Write a Java program that establishes a TCP connection with a mail server through the socket...
Write a Java program that establishes a TCP connection with a mail server through the socket interface, and sends an email message. Your program sends SMTP commands to local mail server, receives and processes SMTP commands from local mail server. It sends email message to one recipient at a time. The email message contains a typical message header and a message body. Here is a skeleton of the code you'll need to write: import java.io.*; import java.net.*; public class EmailSender...
I am trying to write a UDP socket program in which there is a server program...
I am trying to write a UDP socket program in which there is a server program and a client program. This is what I have but I can't figure out where I am going wrong. This is the input and expected output: > gcc udpclient.c –o clientout > gcc udpserver.c –o serverout > ./serverout 52007 ‘to celebrate’ & > ./clientout odin 52007 ‘Today is a good day’ Today is a good day to celebrate //UDP echo client program #include #include...
4. How does a client socket interact with a server socket to exchange data?
4. How does a client socket interact with a server socket to exchange data?
Write a Python Client/Server Socket Program that will allow you to send text messages between the...
Write a Python Client/Server Socket Program that will allow you to send text messages between the server and client, terminating when the exit is typed on the client. Build the program on your 2-VM’s and get it working. Cut and paste your code below along with screen shots of your program working.
. Draw and explain any socket-related activity of the client and server that communicates over the...
. Draw and explain any socket-related activity of the client and server that communicates over the UDP and TCP
DO THIS PROGRAM IN JAVA Write a complete Java console based program following these steps: 1....
DO THIS PROGRAM IN JAVA Write a complete Java console based program following these steps: 1. Write an abstract Java class called Shape which has only one abstract method named getArea(); 2. Write a Java class called Rectangle which extends Shape and has two data membersnamed width and height.The Rectangle should have all get/set methods, the toString method, and implement the abstract method getArea()it gets from class Shape. 3. Write the driver code tat tests the classes and methods you...
URGENT!!! DO THIS PROGRAM IN JAVA Write a complete Java console based program following these steps:...
URGENT!!! DO THIS PROGRAM IN JAVA Write a complete Java console based program following these steps: 1. Write an abstract Java class called Shape which has only one abstract method named getArea(); 2. Write a Java class called Rectangle which extends Shape and has two data membersnamed width and height.The Rectangle should have all get/set methods, the toString method, and implement the abstract method getArea()it gets from class Shape. 3. Write the driver code tat tests the classes and methods...
Please code in C language. Server program: The server program provides a search to check for...
Please code in C language. Server program: The server program provides a search to check for a specific value in an integer array. The client writes a struct containing 3 elements: an integer value to search for, the size of an integer array which is 10 or less, and an integer array to the server through a FIFO. The server reads the struct from the FIFO and checks the array for the search value. If the search value appears in...
in Java - implement ftp-server and ftp-client. ftp-server Logging into ftp-server from ftp-client The ftp-server is...
in Java - implement ftp-server and ftp-client. ftp-server Logging into ftp-server from ftp-client The ftp-server is an interactive, command-line program that creates a server socket, and waits for connections. Once connected, the ftp-client can send and receive files with ftp-server until ftp-client logs out. Sending and receiving files The commands sent from the ftp-client to the ftp-server must recognize and handle are these: rename- the ftp-server responds to this command by renaming the named file in its current directory to...
In Java and using JavaFX, write a client/server application with two parts, a server and a...
In Java and using JavaFX, write a client/server application with two parts, a server and a client. Have the client send the server a request to compute whether a number that the user provided is prime. The server responds with yes or no, then the client displays the answer.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT