please help. About java tcp.
this is what i have so far. how to convert input value into ASCII sequence For example the message: “Hello World” would become “Ifmmp!Xpsme”For example the message: “Hello World” would become “Ifmmp!Xpsme”in server.java.
Client.java:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;
import java.net.SocketTimeoutException;
public class Client {
public static void main(String[] args) throws IOException {
Socket client = new Socket("127.0.0.1", 20006);
client.setSoTimeout(10000);
BufferedReader input = new BufferedReader(new
InputStreamReader(System.in));
PrintStream out = new PrintStream(client.getOutputStream());
BufferedReader buf = new BufferedReader(new
InputStreamReader(client.getInputStream()));
boolean flag = true;
while(flag){
System.out.print("Enter:");
String str = input.readLine();
out.println(str);
if("bye".equals(str)){
flag = false;
}else{
try{
String echo = buf.readLine();
System.out.println(echo);
}catch(SocketTimeoutException e){
System.out.println("Time out, No response");
}
}
}
input.close();
if(client != null){
client.close();
}
}
}
server.java:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;
public class ServerThread implements Runnable {
private Socket client = null;
public ServerThread(Socket client){
this.client = client;
}
@Override
public void run() {
try{
PrintStream out = new PrintStream(client.getOutputStream());
BufferedReader buf = new BufferedReader(new
InputStreamReader(client.getInputStream()));
boolean flag =true;
while(flag){
String str = buf.readLine();
//how to convert input value into ASCII sequence For example the
message: “Hello World” would become “Ifmmp!Xpsme”For example the
message: “Hello World” would become “Ifmmp!Xpsme”
out.println("echo:" + str);
}
}
}
out.close();
client.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
//ServerThread.java
import java.net.*;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;
public class ServerThread implements Runnable
{
private Socket client = null;
public ServerThread(Socket client)
{
this.client = client;
//this should run the thread
this.run();
}
//function that converts string to ascii
sequence
public String formatToAscii(String str)
{
//according to the example
given
//Hello world will become
Ifmmp!Xpsme --> your just incrementing the value of each char by
one
//H + 1 -> I, e + 1
-> f,....
//so this pattern can be achieved
using java string function charAt(pos) gives a char at specified
position.
//it is clear that the incremented
char has no bounds, just incrementing is enough
//because space( ) + 1 -> !
Space ascii is 32 and ! ascii is 33
//so just increment the ascii value
by 1
//and hence update all chars like
this way
//
int i;
String res = "";
for(i =
0;i<str.length();i++)
{
res
+=((char)(str.charAt(i) + 1));
}
return res;
}
@Override
public void run()
{
try
{
PrintStream out = new PrintStream(client.getOutputStream());
BufferedReader buf = new BufferedReader(new
InputStreamReader(client.getInputStream()));
boolean flag
=true;
while(flag)
{
String str = buf.readLine();
//when client sends bye set flag to false
//so it will break the infinite loop
if(str.equals("bye"))
{
System.out.println("echo:" +
str);
flag = false;
}
else
{
//if not
//convert the msg to ascii
sequence by calling formatToAscii
str =
formatToAscii(str);
out.println("echo:" +
str);
}
}
out.close();
client.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
public static void main(String[] args)
{
//you haven't created a server
socket
//without it server wont
create
//below is the server socket
creation code
try
{
ServerSocket ss
= new ServerSocket(1433);
System.out.println("Server is waiting");
//accpts a
client
Socket sa =
ss.accept();
System.out.println("Connected with client
"+sa.getInetAddress());
//creates a
client ServerThread object
ServerThread ob
= new ServerThread(sa);
ss.close();
}
catch(Exception e)
{
}
}
}
//Client.java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;
import java.net.SocketTimeoutException;
public class Client
{
public static void main(String[] args) throws
IOException
{
Socket client = new
Socket("127.0.0.1", 1433);
client.setSoTimeout(10000);
BufferedReader input = new
BufferedReader(new InputStreamReader(System.in));
PrintStream out = new
PrintStream(client.getOutputStream());
BufferedReader buf = new
BufferedReader(new
InputStreamReader(client.getInputStream()));
boolean flag = true;
while(flag)
{
System.out.print("Enter: ");
String str =
input.readLine();
out.println(str);
if("bye".equals(str))
{
flag = false;
}
else
{
try
{
String echo =
buf.readLine();
System.out.println(echo);
}
catch(SocketTimeoutException e)
{
System.out.println("Time out,
No response");
}
}
}
input.close();
if(client != null)
{
client.close();
}
}
}
//sample output
please help. About java tcp. this is what i have so far. how to convert input...
Java Project
Draw a class diagram for the below class (using UML
notation)
import java.io.BufferedReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
public class myData {
public static void main(String[] args) {
String str;
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter text (‘stop’ to quit).");
try (FileWriter fw = new FileWriter("test.txt")) {
do {
System.out.print(": ");
str = br.readLine();
if (str.compareTo("stop") == 0)
break;
str = str + "\r\n"; // add newline
fw.write(str);
} while (str.compareTo("stop") != 0);
} catch (IOException...
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...
I need help with my IM (instant messaging) java program, I created the Server, Client, and Message class. I somehow can't get both the server and client to message to each other. I am at a roadblock. Here is the question below. Create an IM (instant messaging) java program so that client and server communicate via serialized objects (e.g. of type Message). Each Message object encapsulates the name of the sender and the response typed by the sender. You may...
Modify the socket-based date server below so that the server services each client request in a separate thread. import java.net.*; import java.io.*; public class DateClient { public static void main(String[] args) { try { /* make connection to server socket */ Socket sock = new Socket(“127.0.0.1”,6013); InputStream in = sock.getInputStream(); BufferedReader bin = new BufferedReader(new InputStreamReader(in)); /* read the date from the socket */ String line; while ( (line = bin.readLine()) != null) System.out.println(line); /* close the socket connection*/ sock.close(); ...
(I don't need code for this question I only need explanation how to solve the question.) The following code causes an exception error: import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; public class Ex02 { public static void main(String[] args) throws IOException { BufferedReader userInput = new BufferedReader (new InputStreamReader(System.in)); ArrayList<String> myArr = new ArrayList<String>(); myArr.add("Zero"); myArr.add("One"); myArr.add("Two"); myArr.add("Three"); System.out.println(myArr.get(7)); } } Starting with this provided code,...
How to solve this problem by using reverse String and Binary search? Read the input one line at a time and output the current line if and only if it is not a suffix of some previous line. For example, if the some line is "0xdeadbeef" and some subsequent line is "beef", then the subsequent line should not be output. import java.io.BufferedReader; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.List; import java.util.Set; import java.util.TreeSet;...
Java only. Thanks These questions involve choosing the right abstraction (Collection, Set, List, Queue, Deque, SortedSet, Map, or SortedMap) to EFFICIENTLY accomplish the task at hand. The best way to do these is to read the question and then think about what type of Collection is best to use to solve it. There are only a few lines of code you need to write to solve each of them. Unless specified otherwise, sorted order refers to the natural sorted order...
Please complete the give code (where it says "insert code here") in Java, efficiently. Read the entire input one line at a time and then output a subsequence of the lines that appear in the same order they do in the file and that are also in non-decreasing or non-increasing sorted order. If the file contains n lines, then the length of this subsequence should be at least sqrt(n). For example, if the input contains 9 lines with the numbers...
Please answer the question correctly and USE THE MOST EFFICIENT TECHNIQUE OF JAVA COLLECTION FRAME WORK to answer it. It is very essential that you do. Please make sure it is very efficient and doesnt run out of memory. A demo code required for the question is given below. These questions involve choosing the right abstraction (Collection, Set, List, Queue, Deque, SortedSet, Map, or SortedMap) to efficiently accomplish the task at hand. The best way to do these is to...
Please answer question correctly and show the result of the working code. The actual and demo code is provided .must take command line arguments of text files for ex : input.txt output.txt from a filetext(any kind of input for the text file should work as it is for testing the question) This assignment is about using the Java Collections Framework to accomplish some basic text-processing tasks. These questions involve choosing the right abstraction (Collection, Set, List, Queue, Deque, SortedSet, Map,...