Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

here is the code; why isnt it working (ports and stuff)

Status
Not open for further replies.

pollux0

IS-IT--Management
Mar 20, 2002
262
US
Information is being sent to our server on port 4124 and I am trying to build an application that will get the information and put into a file (or even print on the screen).

thanks in advance

***************************
import java.io.*;
import java.net.*;
import java.util.*;

public class DateServer extends Thread {

private ServerSocket dateServer;


public static void main(String argv[]) throws Exception {
new DateServer();
}

public DateServer() throws Exception {
dateServer = new ServerSocket(4124);
System.out.println("Server listening on port 4124.");
this.start();
}

public void run() {
while(true) {
try {
System.out.println("Waiting for connections.");
Socket client = dateServer.accept();
System.out.println("Accepted a connection from: "+
client.getInetAddress());
Connect c = new Connect(client);
} catch(Exception e) {}
}
}
}

class Connect extends Thread {
private Socket client = null;
private InputStream ois = null;
private OutputStream oos = null;

public Connect() {}

public Connect(Socket clientSocket) {
client = clientSocket;
try {
ois = new ObjectInputStream(client.getInputStream());

} catch(Exception e1) {
try {
client.close();
}catch(Exception e) {
System.out.println(e.getMessage());
}
return;
}
this.start();
}


public void run() {
try {
String timestamp=null;
BufferedReader in=new BufferedReader(new InputStreamReader(ois));
System.out.println("Connected to :"+client.getInetAddress()+" on port "+client.getPort());
while(true){
System.out.println("test");
timestamp=in.readLine();
System.out.println(timestamp);
System.out.println("test1");
if(timestamp==null)
{
System.out.println("Sever closed connection");
break;
}
System.out.println(timestamp);
}
}
catch(IOException e){System.out.println(e);}
finally
{

//oos.writeObject(new Date());
//oos.flush();
// close streams and connections
//ois.close();
//oos.close();
//client.close();
}
}


}

 
sorry, I meant:
"STREAMING information is being sent to our server....."
 
What is the data that is being sent to the server?

Currently your code is expecting to received streamed objects -
Code:
 ois = new ObjectInputStream(client.getInputStream());
, so if you are sending to the server bytes or text, this may be the cause of the problem. I have never tried using an Object stream to read text or bytes, so I have no idea what may or may not happen here.

If you are sending bytes to the server try just wrapping the
Code:
 client.getInputStream
in the BufferedReader.

Hope this helps.

----------------------------------------
There are no onions, only magic
----------------------------------------
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top