Question

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 it closes the socket.

Sample Output Client:

Client: Hi there

Server: Hello

Client: How are you?

Server: I am fine

Client: ! Server: Bye

Homework Answers

Answer #1

TCPClient.java :

import java.io.*;

import java.net.*;

class TCPClient

{

public static void main(String argv[]) throws Exception{

String sentence;

String modifiedSentence;

BufferedReader inFromUser = new BufferedReader (new InputStreamReader (System.in));

Socket clientSocket = new Socket("hostname", 1234);

DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());

BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

sentence = inFromUser.readLine();

outToServer.writeBytes(sentence + '\n');

modifiedSentence = inFromServer.readLine();

System.out.println("From SERVER: " + modifiedSentence);

clientSocket.close();

}

}

TCPServer.java :

import java.io.*;

import java.net.*;

class TCPServer {

public static void main(String argv[]) throws Exception

{

String clientSentence;

String capitalizedSentence;

String s;

ServerSocket welcomeSocket = new ServerSocket(1234);

while(true) {

Socket connectionSocket = welcomeSocket.accept();

BufferedReader inFromClient = new BufferedReader (new InputStreamReader (connectionSocket.getInputStream()));

DataOutputStream outToClient = new DataOutputStream (connectionSocket.getOutputStream());

clientSentence = inFromClient.readLine();

capitalizedSentence = clientSentence.toUpperCase() +'\n';

outToClient.writeBytes(capitalizedSentence);

if (s.equalsIgnoreCase("BYE");

break;

}

}

}

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
JAVA: Write a program that takes in a line of text as input, and outputs that...
JAVA: Write a program that takes in a line of text as input, and outputs that line of text in reverse. The program repeats, ending when the user enters “done” (case-insensitive) for the line of text. Note: You are not supposed to use any in-build methods to reverse the text. Example: Enter a series of strings: Hello there Hey done the output is: ereht olleH yeH
Write a C program that prompts the user to enter a line of text on the...
Write a C program that prompts the user to enter a line of text on the keyboard then echoes the entire line. The program should continue echoing each line until the user responds to the prompt by not entering any text and hitting the return key. Your program should have two functions, writeStr and readLn, in addition to the main function. The text string itself should be stored in a char array in main. Both functions should operate on NUL-terminated...
please can you make it simple. For example using scanner or hard coding when it is...
please can you make it simple. For example using scanner or hard coding when it is a good idea instead of arrays and that stuff.Please just make one program (or class) and explain step by step. Also it was given to me a txt.htm 1.- Write a client program and a server program to implement the following simplified HTTP protocol based on TCP service. Please make sure your program supports multiple clients. The webpage file CS3700.htm is provided. You may...
Write a program in C that extracts the tokens from a string. Given a string as...
Write a program in C that extracts the tokens from a string. Given a string as input, the program should print on the screen each token on a new line. For example given the following string as input: “hello there my friends” the program should generate: I am a programmer
1 Design and implement FileCompare program that compares two text input files (file1.txt and file2.txt), line-by-line,...
1 Design and implement FileCompare program that compares two text input files (file1.txt and file2.txt), line-by-line, for equality. Print any lines that are not equivalent indicating the line numbers in both files. The language of implementation is java 2 . Create a program that reads a string input from the user, then determines and prints how many of each lowercase vowels (a, e. i, o, and u) appear in the entire string. Have a separate counter for each vowel. Also...
The code I have written runs but I need it us UDP not TCP. Also I...
The code I have written runs but I need it us UDP not TCP. Also I just need someone to check my work and make sure that it works properly. The python code needs to be run with command line so you can add more than one client. The directions: 1. The chat is performed between 2 clients and not the server. 2. The server will first start up and choose a port number. Then the server prints out its...
IN C LANGUAGE This program initially reads two integers from standard input: (1) an integer T...
IN C LANGUAGE This program initially reads two integers from standard input: (1) an integer T and (2) a positive integer N. It then reads N integers and counts the numbers that are greater than T and the numbers than are less than T. It then prints out these two counts. Example What is the threshold value? 7 How many values? 5 Enter 5 values: 6 7 9 9 8 3 values are greater than 7 1 values are less...
Write an assembly program that reads characters from standard input until the “end of file” is...
Write an assembly program that reads characters from standard input until the “end of file” is reached. The input provided to the program contains A, C, T, and G characters. The file also may have new line characters (ASCII code 10 decimal), which should be skipped/ignored. The program then must print the count for each character. You can assume (in this whole assignment) that the input doe not contain any other kinds of characters.  the X86 assembly program that simply counts...
Write a Java program that reads employee information from the file “employee.txt” then the program will...
Write a Java program that reads employee information from the file “employee.txt” then the program will display his/her information on the screen. The file contains the Employee’s Name, age,join date, quit date, and salary. • For each year the employee is in the company,10 days of sick leave are allowed. So, if an employee is in the company for two years, 20 days of sick leave are allowed. • For each year the employee is in the company, one-month salary...
You will write a program that loops until the user selects 0 to exit. In the...
You will write a program that loops until the user selects 0 to exit. In the loop the user interactively selects a menu choice to compress or decompress a file. There are three menu options: Option 0: allows the user to exit the program. Option 1: allows the user to compress the specified input file and store the result in an output file. Option 2: allows the user to decompress the specified input file and store the result in an...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT