Java网络编程的菜鸟问题
//服务器package Aprilnineteen;
import java.io.*;
import java.net.*;
public class five {
public static void main(String[] args) {
OutputStream w1=null;
InputStream w2=null;
try{
ServerSocket e=new ServerSocket(8888);
Socket q=e.accept();
DataInputStream s1=new DataInputStream(q.getInputStream());
DataOutputStream s2=new DataOutputStream(q.getOutputStream());
System.out.println(s1.readUTF()+q.getPort());
s1.close();
s2.writeUTF("hi");//这个就是five 的16行
s2.flush();
s2.close();
q.close();
}catch(FileNotFoundException e1){
e1.printStackTrace();
}catch(IOException e2){
e2.printStackTrace();
}
}
}
//客户端
import java.io.*;
import java.net.*;
public class six {
public static void main(String[] args) {
OutputStream w=null;
InputStream z=null;
try{
Socket q=new Socket("127.0.0.1",8888);
DataInputStream w1=new DataInputStream(q.getInputStream());
DataOutputStream w2=new DataOutputStream(q.getOutputStream());
w2.writeUTF("hello");
w2.flush();
w2.close();
System.out.println(w1.readUTF());
w1.close();
q.close();
}catch(FileNotFoundException e){
e.printStackTrace();
System.out.println("程序出现错误"+e);
}catch(IOException e1){
e1.printStackTrace();
}
}
这个是控制台发出的问题,没有看懂。小弟感谢啦
java.net.SocketException: Socket closed
at java.net.SocketOutputStream.socketWrite(Unknown Source)
at java.net.SocketOutputStream.write(Unknown Source)
at java.io.DataOutputStream.write(Unknown Source)
at java.io.DataOutputStream.writeUTF(Unknown Source)
at java.io.DataOutputStream.writeUTF(Unknown Source)
at Aprilnineteen.five.main(five.java:16)
hello56478 --------------------编程问答-------------------- 应该要把那些close的语句放到最后面去,如果放前面的话 你已经把IO关闭了 后面的操作就根本不能执行了,最好全部放在finally语句中 --------------------编程问答-------------------- 放到finally里面却出现的提示却是没有处理IOexception,但是已经catch了IOexception,仅仅把close放在finally,还是不对,怎么回事
补充:Java , Java SE