Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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!XpsmeFor example the message: Hello World would become Ifmmp!Xpsmein 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!XpsmeFor example the message: Hello World would become Ifmmp!Xpsme out.println("echo:" + str); } } } out.close(); client.close(); }catch(Exception e){ e.printStackTrace(); } }

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data Management Databases And Organizations

Authors: Richard T. Watson

3rd Edition

0471418455, 978-0471418450

More Books

Students also viewed these Databases questions