message does not get sent correctly?

Hello everyone ,

I have to do a program that captures traffic from a client and sends it to a server and gets the traffic from the server and sends it back to the client... it's a intermediate Java program between a server and a client ( i need it in order to analyze a protocol ) .

In more concrete terms:

1) I have my java program, a windows server and a windows client.

2) My java program connects to the windows server.

3) My java program listens for an incoming connection and accepts the windows client.

4) The java program reads a string from the windows client and send it to windows server. Than it reads from the windows server the response and send it back to the client ... And so on... and it prints the string so that I can analyze the protocol...

The thing is that my program does receive a string from the client and it does send it to the server but it doesn't read the response for some reason ( it gets stuck ) ....

Here is my source code:

import java.io.*;

import java.net.*;

publicclass TrafficCapture{

publicstaticvoid main (String[] args){

ServerSocket trafficCaptureSocket =null;

Socket clientSocket =null;

Socket serverSocket =null;

InputStream fromClient =null;

PrintWriter toClient =null;

InputStream fromServer =null;

PrintWriter toServer=null;

int BUFFER_SIZE = 2048;

int numBytes;

try{

serverSocket =new Socket("127.0.0.1",2000);

}

catch(IOException e){

System.out.println("Could not connect to server.");

System.exit(-1);

}

System.out.println("Connected to server.");

try{

fromServer = serverSocket.getInputStream();

toServer=new PrintWriter ( serverSocket.getOutputStream(),true );

}

catch(IOException e){

System.out.println("Error while getting server input and output streams");

System.exit(-1);

}

try{

trafficCaptureSocket =new ServerSocket(1000);

}

catch (IOException e){

System.out.println("Could not listen.");

System.exit(-1);

}

System.out.println("Waiting for client.");

try{

clientSocket = trafficCaptureSocket.accept();

}

catch (IOException e){

System.out.println("Accept failed");

System.exit(-1);

}

System.out.println("Client connected.");

try{

fromClient = clientSocket.getInputStream();

toClient=new PrintWriter (clientSocket.getOutputStream() ,true );

}

catch(IOException e){

System.out.println("Error while getting client input and output streams.");

System.exit(-1);

}

try{

while (true )

{

byte[] buffer =newbyte[BUFFER_SIZE];

char[] line=newchar[BUFFER_SIZE];

if ( (numBytes = fromClient.read(buffer))!=-1 ){

System.out.println("Received: " + numBytes);

System.out.print("Client to Server>");

for (int i = 0; i < numBytes; i ++ )

{

System.out.print( (char) buffer[i] +",");

line[i] = (char) buffer[i];

}

toServer.write(line, 0 , numBytes);

// toServer.flush();

System.out.println();

}

if ( (numBytes = fromServer.read(buffer))!=-1){

System.out.print("Server to Client>");

for(int i = 0; i < numBytes; i ++ )

{

System.out.print((char) buffer[i] +",");

line[i] = (char) buffer[i];

}

toClient.write(line, 0, numBytes);

// toClient.flush();

System.out.println();

}

}

}

catch(IOException e){

System.out.println("Error while redirecting data.");

}

System.out.println("Out");

try{

clientSocket.close();

serverSocket.close();

trafficCaptureSocket.close();

fromServer.close();

toServer.close();

fromClient.close();

toClient.close();

}

catch(IOException e){

System.out.println("Failed to close sockets.");

}

}

Message was edited by:

HeartOpenBook

[7453 byte] By [HeartOpenBooka] at [2007-11-15]
# 1

Read the different directions in separate threads, wo you don't have to worry about who reads/writes when.

BIJ001a at 2007-7-28 > top of java,Core,Core APIs...