BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Socket closed

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> JBuilder Applet Issues
View previous topic :: View next topic  
Author Message
Davisote
Guest





PostPosted: Wed Apr 13, 2005 11:07 am    Post subject: Socket closed Reply with quote



I have problems trying to read from the client in the class created to
handle every request.
If you see the code in the class InServer, when I try to create my
DataInputStream over the socket I pass in
the constructor if fails, the socket is closed, but I dont know why..
Any help????????????

Here is the code I use to listening and handle the differents request
try {
ss = new ServerSocket(7000);
System.out.println("INIT...n");
System.out.println( "Listening on " + ss );
}catch (Exception e1) {
System.out.println("ERROR: " + e1.getMessage());
}

try {
while (true) {

s = ss.accept();
System.out.println("Socket created: " + s);
try {
new InServer(s).start();
System.out.println("Testing after new");
}
catch(IOException e2) {
// If it fails, close the socket,
// otherwise the thread will close it:
System.out.println("Error: " + e2.getMessage());
s.close();
}
finally {
s.close();
}
}

}catch (Exception e3) {
System.out.println("Error e2: " + e3.getMessage());
}



This is the class I use to handle every request
public class InServer extends Thread implements Runnable {
ServerSocket ss = null;

Socket s = null;

DataInputStream dis = null;
PrintStream ps = null;

OutputStream os = null;

int Port;


public InServer(Socket socket) throws IOException {
s = socket;
System.out.println("Testing if socket is alive: " + s);
}

//Its called automatic when I create in the loop of the server above
public void run() {
System.out.println("Testing if socket if alive: " + s);
//Trying to read
try {
//dis = new DataInputStream(s.getInputStream());
*********************************************************************************
真真真真真真真真真真HERE IS WHERE IT FAIL, IT SAYS SOCKET IS CLOSED,
BUT WHY???????????????????????
os = s.getOutputStream();
**********************************************************************************

//ps = new PrintStream(s.getOutputStream());
//Reading data
String s = dis.readLine();

System.out.println(s);


}catch(Exception e) {
System.out.println("Error: " + e.getMessage());
}

}


This is the class I use in the applet, everything is tested, port and
address

public class OutServer {
private Socket s;
private DataInputStream dis = null;
private DataOutputStream dos=null;

public OutServer(InetAddress addr, int Port) {
System.out.println("Port: " + Port);
System.out.println("Address: " + addr);
try {
s = new Socket(addr, Port);
System.out.println("CONECTION OPEN : " + s);
}
catch (IOException e) {
System.out.println("Socket failed.");
// If the creation of the socket fails,
// nothing needs to be cleaned up.
}
try {
dos = new DataOutputStream(s.getOutputStream());
dis = new DataInputStream(s.getInputStream());
dos.writeBytes("testingEND");

System.out.println(dis.readLine());

}catch(Exception e1) {
System.out.println("Error e1:" + e1.getMessage());
try {
s.close();
} catch(IOException e2) {
System.out.println("Error e2: Socket not closed" + e2.getMessage());
}
}
}





Back to top
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> JBuilder Applet Issues All times are GMT
Page 1 of 1

 
 


Powered by phpBB © 2001, 2006 phpBB Group
.