Results 1 to 5 of 5

Thread: [RESOLVED] reading incoming text on a socket....

  1. #1

    Thread Starter
    Frenzied Member wpearsall's Avatar
    Join Date
    Feb 2002
    Location
    England / UK
    Posts
    1,065

    Resolved [RESOLVED] reading incoming text on a socket....

    OK... I have got a socket i need to read text on...

    the bufferedreader is calling "in", and socket "wskSocket".

    Code:
    	//Take steps to conect to the server
    	synchronized boolean connectToServer()
    	{	
    	
    		if( connected == true ) {
    			disconnectFromServer();			
    		}
    	
    		connected=false;
    		try
    		{
    			InetAddress addr = InetAddress.getByName( serverAddress );
    			wskSocket = new Socket( addr,serverSocketNumber );
    		}
    		catch( UnknownHostException e )
    		{
    			JOptionPane.showMessageDialog(window,"Error: "+sAppTitle+
    										" failed to find server host!",
    										"Host Lookup Error",JOptionPane.ERROR_MESSAGE );
    			return false;
    		}
    		catch( IOException e )
    		{
    			JOptionPane.showMessageDialog(window,"Error: "+sAppTitle+
    										  " failed to connect to the server!","Socket Error",JOptionPane.ERROR_MESSAGE );
    			return false;
    		}
    
    		try
    		{
    			in  = new BufferedReader(new InputStreamReader(wskSocket.getInputStream()));
            	out = new BufferedWriter(new OutputStreamWriter(wskSocket.getOutputStream()));
            	
    		}
    		catch( IOException e )
    		{
    			JOptionPane.showMessageDialog(window,"Error: "+sAppTitle+
    										  " cannot create data stream!\n\n"+
    										  "Closing client...","Data Stream Error",
    										  JOptionPane.ERROR_MESSAGE );
    			try
    			{
    				wskSocket.close();
    			}
    			catch( IOException io_e )
    			{
    				
    			}
    			
    			return false;
    		}
    		
    		connected = true;
    		
    		thListen = new ListenToIncoming();
    		thListen.start();
    		
    		System.out.println("OK: Returning True....");
    		
    		return true;
    
    	}
    	
    
    
    
    	private class ListenToIncoming extends Thread{
    		public void run(){
    			
    			System.out.println("ITS RUNNING!!!!");
    			
    			
    			System.out.println("Sending Login Info...");
    			
    			String sMsgToSend;
    			sMsgToSend=" ## +DL : "+ UserName+" : "+
    						Password+" : "+sAppVer;
    			SendData( sMsgToSend );
    			
    			try{
    				
    				String sIn;
    		        while ((sIn = in.readLine()) != null) {		        	
    					System.out.println( sIn );
    					CheckIncoming( sIn );
    				}
    				
    				System.out.println( "OK... WE JUST SKIPPED THE ENTIRE DATA PROCESSING!!!!" );
    		
    			}catch( IOException e ){
    				System.out.println( "We Got An Error...." );
    			}
    		}
    	}

    for some reason, IT DOES NOT READ INCOMING TEXT!!!!

    it creates the new thread, and starts it running properly, but it wont read it...

    i have got this output on my "System" ....

    OK: Returning True....
    ITS RUNNING!!!!
    Sending Login Info...
    even tho text arrives at the server ok..

    [i know this because its all comin ok on ma server log]

    10/02/2004 00:59:46: Connection requested from 127.0.0.1:2021;
    10/02/2004 00:59:46: Checking Login [Client Version: x.xx] (Connection: 96);
    10/02/2004 00:59:47: -> Login = False [Invalid Password For: wpearsall] (Connection: 96);
    it doesnt recieve the reply from the server [well, doesnt "read" it anyhow...]

    Does anyone know wot can b the problem w/ the above code?

    I have even tried sending data from the server directly... but not got read either

    : shrugs :

    All help is VERY appreciated...

    thanks.
    Wayne
    Wayne

  2. #2

    Thread Starter
    Frenzied Member wpearsall's Avatar
    Join Date
    Feb 2002
    Location
    England / UK
    Posts
    1,065
    oops.. fort u mite also b interested in how im sendin the data to server etc...:

    Code:
    	void SendData( String sData ) {
    		try{
    			out.write( sData ); // Put the data in the queue to send
    			//out.newLine();
    			out.flush();		// Send the queue'd data
    		}catch(Exception m){
    			// Catch error
    		}
    
    	}
    as i said though, this is working...
    Wayne

  3. #3
    Addicted Member MethadoneBoy's Avatar
    Join Date
    Oct 2001
    Location
    Preferably somewhere between Keira Knightley and Diane Kruger but I'm not fussy
    Posts
    180
    It may be because you're not actually getting the text from the stream.

    You need a new BufferedReader object to listen to the "in" object.

    This link should show you what to do:

    http://java.sun.com/docs/books/tutor...ngWriting.html

    Hope this helps
    Last edited by MethadoneBoy; Feb 27th, 2004 at 07:33 AM.
    "'Oh, hello Mr. Crick! What do you think of Jeffrey Archer?' Clip-clip-clip! Oh, come on! Who are you kidding? You wait til I'm mayor, you'll see how tough I am! Christ almighty...."

  4. #4

    Thread Starter
    Frenzied Member wpearsall's Avatar
    Join Date
    Feb 2002
    Location
    England / UK
    Posts
    1,065
    could it be the way that i am sending my data from my server?

    VB Code:
    1. Winsock_Control.SendData(" ## CMD : 1 : 2 : 3 ")

    because i used the following code:

    Code:
    import java.io.*;
    import java.net.*;
    
    public class EchoClient {
        public static void main(String[] args) throws IOException {
    
            Socket echoSocket = null;
            PrintWriter out = null;
            BufferedReader in = null;
    
            try {
                echoSocket = new Socket("localhost", 5065);
                out = new PrintWriter(echoSocket.getOutputStream(), true);
                in = new BufferedReader(new InputStreamReader(
                                            echoSocket.getInputStream()));
            } catch (UnknownHostException e) {
                System.err.println("Don't know about host");
                System.exit(1);
            } catch (IOException e) {
                System.err.println("Couldn't get I/O for "
                                   + "the connection");
                System.exit(1);
            }
    
    		BufferedReader stdIn = new BufferedReader(
                                       new InputStreamReader(System.in));
    		String userInput;
    
    		while ((userInput = stdIn.readLine()) != null) {
    		    out.println(userInput);
    		    System.out.println("echo: " + in.readLine());
    		}
    
    		out.close();
    		in.close();
    		stdIn.close();
    		echoSocket.close();
    		
    		System.out.println("Closed");
        }
    }
    and it doesnt go past / run this code:

    Code:
    System.out.println("echo: " + in.readLine());
    however, when connecting to the Port 7 server, it works brilliant.... [i know data is being sent tho, since my vb client is working perfectly........]

    any help is appriciated!
    Wayne

  5. #5

    Thread Starter
    Frenzied Member wpearsall's Avatar
    Join Date
    Feb 2002
    Location
    England / UK
    Posts
    1,065
    OK... I've got it fixed...

    i needed to edit my server code to add "vbcrlf" to the end of every outgoing message.

    sorted it now tho i jst gotta edit my main chat not to allow new lines in the text otherwise it screws ma java client.. meeh, not much of a biggy.. -> main biggy for me now is to make a new applet -> damn java apps dont seam to be able to be loaded from a web page

    oh well, nm eh LOL.

    tnx tho guys
    Wayne

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