I have three file as shown which are Client, SocketClient and Server.
What the Client does, is just to send a IP address and an Iteration Variable for my personal useage, to the SocketClient.
So my main components are SocketClient and Server, and as you see the code, I can send a message form SocketClient to Server, but i can not send the same message back to server(Actually I have tried, but I have got Exceptions).
So please let e know any solution that you can think of.
Client.java
-------------------------------------------------------------------------
import java.io.*;
public class Client
{
public static void main (
String[] args)
{
SocketClient sc = new SocketClient();
//for console
if (args.length == 2) sc.sendPara(args[0],Integer.parseInt(args[1]));// remote IP address, and iteration numbers
else
if (args.length == 1) sc.sendPara("localhost", Integer.parseInt(args[0]));//local host and iteration numbers
else
System.out.println("Invalid arguments...");
SocketClient.java
---------------------------------------------------------------------------
import java.util.Date;
import java.sql.*;
import java.net.*;
import java.io.*;
public class SocketClient
{
void sendPara(String ipAddress, int iterNum)
{
int port = 8888;//the address of the remote server
String ServerIP = ipAddress;
long startTime = 0, endTime = 0, latencyShot = 0, latencyLong = 0;
try{
InetAddress address = InetAddress.getByName(ServerIP);//get the server by IP address
Socket socket = new Socket(address,port);//initiate a socket object
OutputStream os = socket.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os);
PrintWriter pw = new PrintWriter(osw);//create a PrintWriter object for output
InputStream sIn = socket.getInputStream();
InputStreamReader isr = new InputStreamReader(sIn);
BufferedReader br = new BufferedReader(isr);//create a BufferReader object for input
pw.print("This is written from Client to Server.");
pw.flush();
socket.close();
}catch(IOException e){
System.err.print(e);
}
}
}//end of the class
Server.java
---------------------------------------------------------------------------
import java.net.*;
import java.io.*;
import java.sql.*;
import java.math.*;
public class Server implements Runnable
{//routine - a socket server
int ID;
Socket socket;
public static void main(String[] arg)
{
int port = 8888;
int count = 0;
try{
ServerSocket s = new ServerSocket(port,count);
System.out.println("Waiting on port " + port);
while(true)
{
Socket socket = s.accept();
System.out.println("Connet! ID=" + ++count);
Server server = new Server(socket,count);
System.out.println("before creat thread");
Thread thread = new Thread(server);
System.out.println("before thread");
thread.start();
}
}catch(Exception e){
System.err.println("Server error");
System.err.println(e);
}
}//end of the main()
Server(Socket socket, int ID)
{
this.socket = socket;
this.ID = ID;
}
public void run()
{
System.out.println("thread start");
try
{
InputStream sIn = socket.getInputStream();
InputStreamReader isr = new InputStreamReader(sIn);
BufferedReader br = new BufferedReader(isr);
OutputStream sOut = socket.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(sOut);
PrintWriter pw = new PrintWriter(osw);//create a PrintWriter object for output
String inString = br.readLine();
System.out.println("inString: " + inString);
while (inString != null)
{
pw.println(inString);
pw.flush();
inString = br.readLine();
}
}catch (Exception e){
System.err.println(e);
}finally{// Close all objects
try
{
socket.close();
}catch(IOException e){
System.out.println(e);
}
System.out.println("Disconnect! ID=" + ID);
}
}//end of the thread run()
}//end of the class