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

[6222 byte] By [lokiea] at [2007-11-27 10:01:01]
# 1

I posted more compact code so its easier to see and understand: The run() function is where the problem is occurring.

//SAMPLE OUTPUT FROM CONSOLE

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=2750,localport=3333] connected.

hostname: localhost

Ip address: 127.0.0.1:3333

Event from: localhost-> This is a test

Event from: localhost-> his is a test

Event from: localhost-> his is a test

Connection closed by client.

//END OF SAMPLE OUTPUT--//

//THis class is called everytime a new thread is created,

//only 1 thread is created because only 1 client is connecting to the

//server

package server;

//parser server

import java.io.IOException.*;

import java.io.*;

import java.net.*;

import java.util.Scanner;

public class MultiThreadServer implements Runnable {

Socket csocket;

MultiThreadServer(Socket csocket) {

this.csocket = csocket;

}

public void run() {

try {

//setting up channel to recieve events from the omnibus server

BufferedReader in= new BufferedReader(new InputStreamReader(csocket.getInputStream()));

String input;

//accepting events from omnibus server and storing them

//in a string for later processing.

while ((input = in.readLine()) != null)

{

//accepting and printing out events from omnibus server

//also printing out connected client information

System.out.println("Event from: " + csocket.getInetAddress().getHostName()

+"-> "+ input +"\n");

System.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();

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);

}

}

}

//This is the main function, the only thing it does is

//create a new thread on each connection.

package server;

//This is the parser Client that will parse messages and send to the

//z/Os output Server

import java.io.*;

import java.net.*;

public class MainTest

{

public static void main(String args[]) throws Exception

{

//get console input

BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));

System.out.println("Enter port to run server on: ");

String input = stdin.readLine();

int servPort = Integer.parseInt(input);

//setting up sockets

ServerSocket ssock = null;

Socket sock = null;

try

{

//setting up server to run on servPort

ssock = new ServerSocket(servPort);

System.out.println("Listening on : " + ssock);

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

while (true) {

//waiting for client to connect to server socket

sock = ssock.accept();

System.out.println(sock + " connected.");

//get host information

InetAddress clientIa = sock.getInetAddress( );

String clientName = clientIa.getHostName();

String clientIp = clientIa.getHostAddress();

int localPort = sock.getLocalPort();

System.out.println("hostname: "+clientName +

'\n' + "Ip address: " + clientIp

+":"+ localPort + "\n\n");

new Thread(new MultiThreadServer(sock)).start();

}

}

catch (SocketException e )

{

System.out.println ("Socket error: " + e);

}

catch(UnknownHostException e)

{

System.out.println("Unknown host: " + e);

}

catch (IOException e)

{

System.out.println("IOException: " + e);

}

}

}

lokiea at 2007-7-13 0:32:59 > top of Java-index,Core,Core APIs...
# 2

n/m I got it, for some reason this check screwed it:

if(in.read() == -1)

{

System.out.println("Connection closed by client.");

break;

}

lokiea at 2007-7-13 0:32:59 > top of Java-index,Core,Core APIs...
# 3
The reason is that if that didn't return -1 you just threw away a byte.
ejpa at 2007-7-13 0:32:59 > top of Java-index,Core,Core APIs...