once I send 1 string, the rest are cut off, any ideas?
Hello everyone.
For some reason when I send my first string through the socket, everything works fine. But after that Its cutting off the first 2 or 3 characters of the string and I have no idea why because I'm flushing the output buffer before and after I send the the string to the output buffer.
note: complete code can be found here:
http://cpp.sourceforge.net/?show=38132
Am I not flushing right?
Here's the code:
publicclass MultiThreadServerimplements Runnable{
//csocket is whatever client is connecting to this server, in this case it will
//be a telnet client sending strings to the server.
Socket csocket;
MultiThreadServer(Socket csocket){
this.csocket = csocket;
}
publicvoid run(){
//setting up sockets
Socket outputServ =null;
//create a message database to store events
MessageDB testDB =new MessageDB();
try{
//setting up channel to recieve events from the omnibus server
BufferedReader in=new BufferedReader(
new InputStreamReader(
csocket.getInputStream()));
PrintWriter out2
=new PrintWriter(
new OutputStreamWriter(
csocket.getOutputStream()));
//This socket will be used to communicate with the z/OS reciever
//we will need a new socket each time because this is a multi-threaded
//server thus, the z/OS reciever (outputServ) will need to be
//multi threaded to handle all the output.
outputServ =new Socket("localhost",1234);
if(outputServ.getLocalSocketAddress() ==null)
System.out.println("OutputServer isn't running...");
//Setting up channel to send data to outputserv
PrintWriter out
=new PrintWriter(
new OutputStreamWriter(
outputServ.getOutputStream()));
String input;
//accepting events from omnibus server and storing them
//in a string for later processing.
while ((input = in.readLine()) !=null)
{
//looking to see what telnet gets
out2.println(input);
out2.flush();
//accepting and printing out events from omnibus server
//also printing out connected client information
System.out.flush();
System.out.println("Event from: " + csocket.getInetAddress().getHostName()
+"-> "+ input +"\n");
System.out.flush();
//sending events to output server
out.flush();
out.println(input);
out.flush();
//close the connection if the client drops
if(in.read() == -1)
{
System.out.println("Connection closed by client.");
break;
}
}
//cleaning up
in.close();
out.close();
outputServ.close();
csocket.close();
}
catch (SocketException e )
{
System.err.println ("Socket error: " + e);
}
catch(UnknownHostException e)
{
System.out.println("Unknown host: " + e);
}
catch (IOException e)
{
System.out.println("IOException: " + e);
}
}
}
Here's some example output I'm getting from the telnet screen which is (out2) stream:
This is a test
This is another test
his is another test
This is yet another test
his is yet another test
This is a test worked fine, as you can see, it outputed This is a test when I typed it in.
The next string "This is another test" fails, as you can see its echoing out:
his is another test
I'm also printing out the contents to the console, and i'm getting out the same thing:
Enter port to run server on:
3333
Listening on : ServerSocket[addr=0.0.0.0/0.0.0.0,port=0,localport=3333]
Waiting for client connection...
Socket[addr=/127.0.0.1,port=2184,localport=3333] connected.
hostname: localhost
Ip address: 127.0.0.1:3333
Event from: localhost-> This is a test
Event from: localhost-> his is another test
Event from: localhost-> his is yet another test
Connection closed by client.
ANy help would be great!
Message was edited by:
lokie

