当前位置: 技术问答>java相关
Java ServerSocket编程,我为什么接收不到数据?
来源: 互联网 发布时间:2015-02-04
本文导语: try{ SockIn=new BufferedReader(new InputStreamReader(the_Sock.getInputStream())); SockOut=new DataOutputStream(the_Sock.getOutputStream()); System.out.println("clt_thread:create"); while ((ss=SockIn.readLine())!=null){ System.out.println(ss); } the...
try{
SockIn=new BufferedReader(new InputStreamReader(the_Sock.getInputStream()));
SockOut=new DataOutputStream(the_Sock.getOutputStream());
System.out.println("clt_thread:create");
while ((ss=SockIn.readLine())!=null){
System.out.println(ss);
}
the_Sock.close();
}catch(IOException ioe_err){
System.out.println("输出错误:"+ioe_err);
}
环境:JB5
the_Sock是我ServerSocket.accept()得来的。
打印SS要到客户端短开时才会有,我的代码和样例的好象差不多
请问我的代码为什么不能适时的收到客户端发来的数据包?
SockIn=new BufferedReader(new InputStreamReader(the_Sock.getInputStream()));
SockOut=new DataOutputStream(the_Sock.getOutputStream());
System.out.println("clt_thread:create");
while ((ss=SockIn.readLine())!=null){
System.out.println(ss);
}
the_Sock.close();
}catch(IOException ioe_err){
System.out.println("输出错误:"+ioe_err);
}
环境:JB5
the_Sock是我ServerSocket.accept()得来的。
打印SS要到客户端短开时才会有,我的代码和样例的好象差不多
请问我的代码为什么不能适时的收到客户端发来的数据包?
|
试试这个
// The server:
import java.net.*;
import java.io.*;
import java.util.Date;
/**
* A class send out date infomation through socket.
*
* @author: java8964
* @version: 1
*/
public class DaytimeServer {
public static final int PORT = 1300;
public static void main(String[] args) {
try {
ServerSocket server = new ServerSocket(PORT);
Socket connection = null;
while (true) { // using a while (true) loop
try
{
connection = server.accept();
OutputStreamWriter out =
new OutputStreamWriter(connection.getOutputStream());
Date now = new Date();
out.write(now.toString() + "rn");
out.flush();
connection.close();
} catch (IOException ioe) {
System.err.println(ioe);
} finally {
try {
if (connection != null)
connection.close();
} catch (IOException e) {}
}
}
} catch (IOException ee) {
System.err.println(ee);
}
} // end main
} // end DaytimeServer
// The client
import java.net.*;
import java.io.*;
/**
* A class to receive time infomation from server through socket.
*
* @author: java8964
* @version: 1
*/
public class DaytimeClient {
/**
* The port which client and server make connection.
*/
public static final int PORT = 1300;
public static void main(String[] args) {
String hostName;
if (args.length > 0) {
hostName = args[0];
} else {
hostName = "localhost";
}
try {
Socket clientSocket = new Socket(hostName, PORT);
InputStream timeStream = clientSocket.getInputStream();
StringBuffer time = new StringBuffer();
int c;
while ((c = timeStream.read()) != -1) {
time.append((char)c);
}
String timeString = time.toString().trim();
System.out.println("It is " + timeString + " at " + hostName);
} catch (UnknownHostException ukhe) {
System.err.println(ukhe);
} catch (IOException ioe) {
System.err.println(ioe);
}
} // end main
} // end DaytimeClient
// The server:
import java.net.*;
import java.io.*;
import java.util.Date;
/**
* A class send out date infomation through socket.
*
* @author: java8964
* @version: 1
*/
public class DaytimeServer {
public static final int PORT = 1300;
public static void main(String[] args) {
try {
ServerSocket server = new ServerSocket(PORT);
Socket connection = null;
while (true) { // using a while (true) loop
try
{
connection = server.accept();
OutputStreamWriter out =
new OutputStreamWriter(connection.getOutputStream());
Date now = new Date();
out.write(now.toString() + "rn");
out.flush();
connection.close();
} catch (IOException ioe) {
System.err.println(ioe);
} finally {
try {
if (connection != null)
connection.close();
} catch (IOException e) {}
}
}
} catch (IOException ee) {
System.err.println(ee);
}
} // end main
} // end DaytimeServer
// The client
import java.net.*;
import java.io.*;
/**
* A class to receive time infomation from server through socket.
*
* @author: java8964
* @version: 1
*/
public class DaytimeClient {
/**
* The port which client and server make connection.
*/
public static final int PORT = 1300;
public static void main(String[] args) {
String hostName;
if (args.length > 0) {
hostName = args[0];
} else {
hostName = "localhost";
}
try {
Socket clientSocket = new Socket(hostName, PORT);
InputStream timeStream = clientSocket.getInputStream();
StringBuffer time = new StringBuffer();
int c;
while ((c = timeStream.read()) != -1) {
time.append((char)c);
}
String timeString = time.toString().trim();
System.out.println("It is " + timeString + " at " + hostName);
} catch (UnknownHostException ukhe) {
System.err.println(ukhe);
} catch (IOException ioe) {
System.err.println(ioe);
}
} // end main
} // end DaytimeClient