2 dingding1xue dingding1xue 于 2014.04.21 17:54 提问

java socket简短编程问题

** 客户端代码: **
import java.io.*;
import java.net.*;
public class ding {

/**
 * @param args
 */
public static void main(String[] args) throws Exception {
    // TODO Auto-generated method stub
    Socket socket1=new Socket("localhost",18080);
    byte[] by=new byte[1024];
    InputStream s=new FileInputStream("E:/cing.txt");
    BufferedInputStream a=new BufferedInputStream(s);
    BufferedOutputStream b=new BufferedOutputStream(socket1.getOutputStream());
    while(a.read(by)!=-1)
        b.write(by);
    System.out.println("abc");
    a.close();
    b.close();



}

}

** 服务端代码如下 :**`

import java.io.*;
import java.net.*;
public class fw {

/**
 * @param args
 */
public static void main(String[] args) throws IOException{
    // TODO Auto-generated method stub
    byte[] by=new byte[1024];
 ServerSocket socket=new ServerSocket(18080,10,InetAddress.getByName("localhost"));
 Socket s=socket.accept();
 InputStream in=s.getInputStream();
 FileOutputStream out=new FileOutputStream("abc.txt"); 
 int len=0;
 while((len=in.read(by))!=-1)
     out.write(by,0,len);
 in.close();
 out.close();
 socket.close();
}

}

总是抛出异常,谁帮忙运行一下,帮忙解决,小弟刚在学习java socket,各位帮帮忙吧!

Csdn user default icon
上传中...
上传图片
插入图片