D
Dzohnny
Gast
Hi,
ich habe folgendes Problem:
Ich bin gerade dabei mich ein wenig mit Socket auseinanderzusetzen. Dafür habe ich foldenen Code aus meinem Tut ein wenig modifiziert:
Server:
Client:
Problem ist nun, dass nachdem ich dem Client ein "Ok" zurücksende, kommt als Input beim Server nur noch "null" an, als Endlosshleife und ich kann auch kein zweites mal mich mit dem Server verbinden. Was mache ich da falsch?
ich habe folgendes Problem:
Ich bin gerade dabei mich ein wenig mit Socket auseinanderzusetzen. Dafür habe ich foldenen Code aus meinem Tut ein wenig modifiziert:
Server:
Code:
ServerSocket echoServer = null;
String line;
DataInputStream is;
PrintStream os;
Socket clientSocket = null;
// Try to open a server socket on port 9999
// Note that we can't choose a port less than 1023 if we are not
// privileged users (root)
try {
echoServer = new ServerSocket(9999);
}
catch (IOException e) {
System.out.println(e);
}
// Create a socket object from the ServerSocket to listen and accept
// connections.
// Open input and output streams
try {
clientSocket = echoServer.accept();
is = new DataInputStream(clientSocket.getInputStream());
os = new PrintStream(clientSocket.getOutputStream());
// As long as we receive data, echo that data back to the client.
while (true) {
line = is.readLine();
//os.println(line);
// if(line != null)
System.out.println(line);
os.println("Ok");
}
}
catch (IOException e) {
System.out.println(e);
}
}
Client:
Code:
Socket smtpSocket = null;
DataOutputStream os = null;
DataInputStream is = null;
// Initialization section:
// Try to open a socket on port 25
// Try to open input and output streams
try {
smtpSocket = new Socket("127.0.0.1", 9999);
os = new DataOutputStream(smtpSocket.getOutputStream());
is = new DataInputStream(smtpSocket.getInputStream());
} catch (UnknownHostException e) {
System.err.println("Don't know about host: hostname");
} catch (IOException e) {
System.err.println("Couldn't get I/O for the connection to: hostname");
}
// If everything has been initialized then we want to write some data
// to the socket we have opened a connection to on port 25
if (smtpSocket != null && os != null && is != null) {
try {
// The capital string before each colon has a special meaning to SMTP
// you may want to read the SMTP specification, RFC1822/3
os.writeBytes("HELO\n");
os.writeBytes("MAIL From: [email]k3is@fundy.csd.unbsj.ca[/email]\n");
os.writeBytes("RCPT To: [email]k3is@fundy.csd.unbsj.ca[/email]\n");
os.writeBytes("DATA\n");
os.writeBytes("From: [email]k3is@fundy.csd.unbsj.ca[/email]\n");
os.writeBytes("Subject: testing\n");
os.writeBytes("Hi there\n"); // message body
os.writeBytes("\n.\n");
os.writeBytes("QUIT");
// keep on reading from/to the socket till we receive the "Ok" from SMTP,
// once we received that then we want to break.
String responseLine;
while ((responseLine = is.readLine()) != null) {
System.out.println("Server: " + responseLine);
if (responseLine.indexOf("Ok") != -1) {
break;
}
}
// clean up:
// close the output stream
// close the input stream
// close the socket
System.out.println("schliesse den gnzen kram");
os.close();
is.close();
smtpSocket.close();
} catch (UnknownHostException e) {
System.err.println("Trying to connect to unknown host: " + e);
} catch (IOException e) {
System.err.println("IOException: " + e);
}
}
Problem ist nun, dass nachdem ich dem Client ein "Ok" zurücksende, kommt als Input beim Server nur noch "null" an, als Endlosshleife und ich kann auch kein zweites mal mich mit dem Server verbinden. Was mache ich da falsch?