This program consists of a client1 named client1 and a server named server.My client is able to connect to the server but the server cannot catch what my client is sending as inputs.
Here are the codes
client codes
<code>
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.io.*;
import java.util.Scanner;
public class client1 {
public static void main(String[] args) {
/** Define a host server */
String host = "localhost";
/** Define a port */
int port = 29999;
StringBuffer instr = new StringBuffer();
String TimeStamp;
System.out.println("SocketClient initialized");
Scanner sc=new Scanner(System.in);
try {
Socket connection = new Socket(host, port);
PrintWriter osw = new PrintWriter(connection.getOutputStream());
TimeStamp = new java.util.Date().toString();
BufferedReader bf=new BufferedReader(new InputStreamReader(connection.getInputStream()));//delete this line
String server_chat;;
String user_chat;
osw.println("sending client input to server");
while ((( sc.hasNextLine())) && (!(user_chat=sc.nextLine()).equals("quit")) && (((server_chat=bf.readLine())!=null) )){
System.out.println("Server says"+user_chat);
osw.println("sending client input to server");
}
}
catch (IOException f) {
System.out.println("IOException: " + f);
}
catch (Exception g) {
System.out.println("Exception: " + g);
}
}
}
</code>
server codes
<code>
//package bdn;
import java.net.*;
import java.io.*;
import java.util.*;
public class server implements Runnable {
private Socket connection;
private String TimeStamp;
private int ID;
public static void main(String[] args) {
int port = 29999;
int count = 0;
try{
ServerSocket socket1 = new ServerSocket(port);
System.out.println("MultipleSocketServer Initialized");
// while (true) {
Socket connection = socket1.accept();
server runnable = new server(connection, ++count);
Thread thread = new Thread(runnable);
thread.start();
// }
}
catch (Exception e) {System.out.println(e.getMessage());}
}
server(Socket s, int i) {
this.connection = s;
this.ID = i;
}
public void run() {
System.out.println("Testing run ok");
try {
String character;
PrintWriter osw = new PrintWriter(connection.getOutputStream());
// StringBuffer process = new StringBuffer();
BufferedReader bf=new BufferedReader(new InputStreamReader(connection.getInputStream()));
while(((character = bf.readLine()) !=null)) {
System.out.println("Server is writing");
osw.print("character");
}
}
catch (Exception e) {
System.out.println(e);
}
finally {
try {
connection.close();
}
catch (IOException e){System.out.println(e);}
}
}
}
</code>
Where have I erred in the codes and how can I improve them?
Thanks in adv