PDA

Click to See Complete Forum and Search --> : [RESOLVED] reading incoming text on a socket....


wpearsall
Feb 9th, 2004, 06:59 PM
OK... I have got a socket i need to read text on...

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



//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

wpearsall
Feb 9th, 2004, 07:02 PM
oops.. fort u mite also b interested in how im sendin the data to server etc...:

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...

MethadoneBoy
Feb 26th, 2004, 10:05 AM
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/tutorial/networking/sockets/readingWriting.html

Hope this helps :)

wpearsall
Feb 29th, 2004, 11:18 AM
could it be the way that i am sending my data from my server?


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


because i used the following 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:

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!

wpearsall
Feb 29th, 2004, 11:53 AM
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