java--socket编程,报错为 java.net.SocketInputStream.read(Unknown Source),怎么回事啊?
服务器代码:publicclassServer{publicstaticvoidmain(Stringargs[]){ServerSocketserver=null;So...
服务器代码:public class Server {
public static void main(String args []){
ServerSocket server=null;
Socket client=null;
System.out.println("2号位置");
while(true){
try {
server=new ServerSocket(2014);
System.out.println("1号位置");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
client=server.accept();
System.out.println("3号位置");
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
if(client!=null){
new ServerThread(client).start();
System.out.println("4号位置");
}
try {
server.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
class ServerThread extends Thread{
Socket socket;
DataOutputStream out=null;
DataInputStream in=null;
String s;
ServerThread(Socket s){
socket=s;
try {
out=new DataOutputStream(socket.getOutputStream());
in=new DataInputStream(socket.getInputStream());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void run(){
while(true){
try {
double r=in.readDouble(); //就是这一行报的错
double area=Math.PI*r*r;
out.writeDouble(area);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
重新运行一次后,报错的位置又变了 展开
public static void main(String args []){
ServerSocket server=null;
Socket client=null;
System.out.println("2号位置");
while(true){
try {
server=new ServerSocket(2014);
System.out.println("1号位置");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
client=server.accept();
System.out.println("3号位置");
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
if(client!=null){
new ServerThread(client).start();
System.out.println("4号位置");
}
try {
server.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
class ServerThread extends Thread{
Socket socket;
DataOutputStream out=null;
DataInputStream in=null;
String s;
ServerThread(Socket s){
socket=s;
try {
out=new DataOutputStream(socket.getOutputStream());
in=new DataInputStream(socket.getInputStream());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void run(){
while(true){
try {
double r=in.readDouble(); //就是这一行报的错
double area=Math.PI*r*r;
out.writeDouble(area);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
重新运行一次后,报错的位置又变了 展开
2个回答
展开全部
public class Server {
public static void main(String args []){
ServerSocket server=null;
Socket client=null;
System.out.println("2号位置");
while(true){
try {
//你在这里new serverSocket也就是说每次有客户端连接都会申请新的端口2014;
//你试过有两个客户端连接的时候会不会报错?
server=new ServerSocket(2014);
System.out.println("1号位置");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
client=server.accept();
System.out.println("3号位置");
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
if(client!=null){
new ServerThread(client).start();
System.out.println("4号位置");
}
try {
server.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
class ServerThread extends Thread{
Socket socket;
DataOutputStream out=null;
DataInputStream in=null;
String s;
ServerThread(Socket s){
socket=s;
try {
out=new DataOutputStream(socket.getOutputStream());
in=new DataInputStream(socket.getInputStream());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void run(){
while(true){
try {
//这里是个死循环!!!终归是不科学的;
//你没有给出客户端的代码 所以也没办法知道具体应该怎么写;
double r=in.readDouble(); //就是这一行报的错
double area=Math.PI*r*r;
out.writeDouble(area);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void main(String args []){
ServerSocket server=null;
Socket client=null;
System.out.println("2号位置");
while(true){
try {
//你在这里new serverSocket也就是说每次有客户端连接都会申请新的端口2014;
//你试过有两个客户端连接的时候会不会报错?
server=new ServerSocket(2014);
System.out.println("1号位置");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
client=server.accept();
System.out.println("3号位置");
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
if(client!=null){
new ServerThread(client).start();
System.out.println("4号位置");
}
try {
server.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
class ServerThread extends Thread{
Socket socket;
DataOutputStream out=null;
DataInputStream in=null;
String s;
ServerThread(Socket s){
socket=s;
try {
out=new DataOutputStream(socket.getOutputStream());
in=new DataInputStream(socket.getInputStream());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void run(){
while(true){
try {
//这里是个死循环!!!终归是不科学的;
//你没有给出客户端的代码 所以也没办法知道具体应该怎么写;
double r=in.readDouble(); //就是这一行报的错
double area=Math.PI*r*r;
out.writeDouble(area);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
在执行读之前,应该先判断是否有数据可读吧?!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询