HENRY::--
I guess the code will not be required in my case but still I will add that
I am talking this in context of Client.java
I am using 4 classes in my exact program. First class is main
2nd:- is initiating socket connection, which are further used by both other classes..
3rd:- This class is meant for Catching the keys pressed or input given..Then use in its interface and sends to server as well..
4th:- This is meant to recieve Data from the server..
Now for this I have tried different structures in my program but in each and every I failed..
I want to run both simultaniously(Recieving and sending)
Thus I have planned to use thread for this..
If I use single class then only a single run method will be there and won't be able to run 2 methods.
So I have planned to use 2 classes.
My code is as follows
Client.java
import java.io.*;
import java.net.*;
public class Client
{
public static void main(
String[] s)
{
CreateSocket CCS = new CreateSocket();
Recieve RC = new Recieve();
ClientSend CSend = new ClientSend();
try
{
CCS.makeConn();
(new Thread(RC)).start();
(new Thread(CSend)).start();
}
catch(IOException e)
{
System.err.println("There is some error");
}
//CCS.data();
}
}
class CreateSocket
{
PrintWriter out = null;
BufferedReader in, stdIn = null;
Socket CSocket = null;
//BufferedReader Stdin = new BufferedReader(new InputStreamReader(System.in));
public void makeConn() throws IOException
{
try
{
CSocket = new Socket("127.0.0.1", 4141);
}
catch(UnknownHostException e)
{
System.err.println("Could not Find Localhost");
}
catch(IOException e)
{
System.err.println("Could not Make IO connection to localhost");
}
}
}
class Recieve extends CreateSocket implements Runnable
{
public void sendData()
{
String toServer,fromServer = null;
try
{
in = new BufferedReader(new InputStreamReader(CSocket.getInputStream()));
out = new PrintWriter(CSocket.getOutputStream(), true);
stdIn = new BufferedReader(new InputStreamReader(System.in));
while((toServer=stdIn.readLine()) != null)
{
System.out.println("Client: "+toServer);
out.print(toServer);
toServer = stdIn.readLine();
}
}
catch(IOException e)
{
System.err.println("Some Unreported IOException");
}
}
public void run()
{
sendData();
}
}
class ClientSend extends CreateSocket implements Runnable
{
public void data()
{
String fromServer;
try
{
in = new BufferedReader(new InputStreamReader(CSocket.getInputStream()));
while((fromServer=in.readLine()) != null)
{
if(fromServer == "Quit")
{
break;
}
System.out.println("Server : " + fromServer);
}
}
catch (IOException e)
{
System.err.println("This is an error");
}
}
public void run()
{
data();
}
}
Server.java
import java.net.*;
import java.io.*;
public class Server
{
public static void main(String[] s)
{
CreateSocket CS = new CreateSocket();
////CS.sendData();
try
{
CS.makeConn();
Thread t1 = new Thread(CS);
t1.start();
}
catch(IOException e)
{
System.err.println("There is some problem");
}
}
}
class CreateSocket implements Runnable
{
ServerSocket serverSocket = null;
Socket clientSocket= null;
public void makeConn() throws IOException
{
try
{
serverSocket = new ServerSocket(4141);
}
catch(IOException e)
{
System.err.println("Port 4444 doesn't seem to be opened");
System.exit(1);
}
try
{
clientSocket= serverSocket.accept();
}
catch (IOException e)
{
System.err.println("Accept Failed");
}
}
PrintWriter out=null;
public void sendData()
{
String outLine = null;
try
{
BufferedReader stdIn = null;
PrintWriter out = new PrintWriter(clientSocket.getOutputStream() , true);
outLine = "This is send by server";
out.println(outLine);
stdIn = new BufferedReader(new InputStreamReader(System.in));
while(outLine != "Quit")
{
outLine = stdIn.readLine();
if(outLine != null)
{
System.out.print("\r\n Server: "+ outLine);
out.println(outLine);
}
}
}
catch(IOException e)
{
System.err.println("Some Problem");
}
}
//public void recieveData()
//{
// String inLine = null;
//}
public void run()
{
sendData();
}
}