CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Threaded View

  1. #1
    Join Date
    Sep 2004
    Posts
    236

    Arrow for some reason, after I send my first string via socket, the rest are cutt off?

    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.

    Here is my sample output:
    Code:
    //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-----//

    Am I not flushing right?

    Here's the code:
    Code:
     
     
    //---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);
    	    }
     
    	}
    }
     
    




    Code:
    //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);
    		    }
     
    	  }
    }
    Last edited by voidflux; July 8th, 2007 at 07:10 PM.
    Computer Science/Engineering
    @ PSU
    Co-oping with IBM's zSeries team! wee
    VS 2005.net
    http://www.personal.psu.edu/css204/

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured