下面是一个使用Socket通信的程序,实现服务器端fileServer向客户端fileClient传送一个文件,传送完毕后程序结束,服务器端IP为202.116.223.56,服务器端程序和客户端不在同一电脑。请填空:(一共五个空) 
//服务器端程序 
import java.net.*; 
import java.io.*; 
class fileServer 
{  OutputStream os;  BufferedOutputStream  bw; 
  String downFile;    Socket client; 
  public fileServer(String downFile){ 
  try{ 
ServerSocket ss=new ServerSocket(1234); 
client=                            ; ①    //等待客户 
  this.downFile=downFile; 
    os=client.getOutputStream();    bw=new BufferedOutputStream(os); 
  } 
  catch(Exception e){System.out.println(e.toString());} 
  } 
  public void sendFile(){ 
  try{                                    //生成downFile的文件字节输入流并对其进行缓冲 
BufferedInputStream stdin= new BufferedInputStream(new                              ); ② 
  int count;  byte[] ch=new byte[256];    System.out.println("开始传送"); 
  while((count=stdin.read(ch))!=-1){ 
bw.                            ; ③    //将ch数组中前count个字节写到bw中 
bw.flush();  } 
  System.out.println("传送结束"); 
  bw.close();  os.close(); 
    } 
  catch(Exception e){System.out.println(e.toString());} 
  } 
public static void main(String[] args) { 
try{ fileServer s=new fileServer(args[0]); 
s.sendFile(); 
} catch(Exception e){System.out.println(e.toString());} 


//客户端程序 
import java.net.*; 
import java.io.*; 
class fileClient extends Thread 
{  InputStream is;  BufferedInputStream br; 
  Socket client;  String downFile; 
public fileClient(String downFile){ 
try{ 
    this.downFile=downFile; 
client=new                              ; ④//Socket对象连接服务器202.116.223.56端口1234 
  is=client.getInputStream(); 
  br=new  BufferedInputStream(is); 
  } 
    catch(Exception e){e.printStackTrace();} 

public void receive(){ 
try{                              //生成downFile的文件字节输出流并对其进行缓冲 
  BufferedOutputStream bos= new BufferedOutputStream(new                            );⑤ 
int count;  byte[] ch=new byte[256];  System.out.println("开始接收"); 
  while((count=br.read(ch)) != -1){ 
      bos.write(ch);  bos.flush(); 

  System.out.println("接收完毕"); 
  bos.close();  br.close();  is.close(); 
  } 
  catch(Exception e){  System.out.println(e.toString());  } 
  } 
public static void main(String[] args) 
{ fileClient c=new fileClient(args[0]); 
c.receive(); 

解决方案 »

  1.   


    import java.net.*;
    import java.io.*;
    import java.util.*;class Server {
    boolean started = false;
    ServerSocket ss = null;
    List<ClientRun> list = new ArrayList<ClientRun>();
    public static void main(String[] args) {
    new Server().start();
    } public void start()
    {
    try
    {
    ss = new ServerSocket(8888);
    started = true;
    }
    catch (BindException be)
    {
    System.out.println("端口使用中!");
    }
    catch (IOException e)
    {
    System.out.println("服务器连接失败!");
    }
    try
    {
    while (started) {
    Socket s = ss.accept();
    ClientRun cr = new ClientRun(s);        //主线程只负责接收信息,每个客户端连接进来都会开始一个新线程
    new Thread(cr).start();    //把连接进来的Socket传到线程中。
    list.add(cr);
    System.out.println("系统提示:"+s.getInetAddress()+"已经联入");
    }
    }
    catch (IOException e)
    {
    System.out.println("Client closed!");
    }
    }
    /***********************************多线程实现多客户端同时连接**************************************************/
    class ClientRun implements Runnable
    {
    private Socket s;
    private DataInputStream dis = null;
    private DataOutputStream dos = null;
    boolean bConnect = false;
    public ClientRun(Socket s)                        
    {
    this.s = s;
    try
    {
    dis = new DataInputStream(s.getInputStream());   
    dos = new DataOutputStream(s.getOutputStream());
    bConnect = true;
    }
    catch (IOException e)
    {
    e.printStackTrace();
    }
    } public void send(String str)
    {
    try
    {
    dos.writeUTF(str);
    }
    catch (IOException e)
    {
    e.printStackTrace();
    }

    }
    public void run()
    {
    try
    {
    while (bConnect)
    {
    String str = dis.readUTF();
    System.out.println(str);
    for (int i=0 ;i<list.size() ;i++ )
    {
    ClientRun cr = list.get(i);
    cr.send(str);
    }
    }
    }
    catch (Exception e)
    {
    System.out.println(s.getInetAddress()+"离开了!");
    }
    finally
    {
    try
    {
    if (dis != null) dis.close();
    if (dos != null) dos.close();
    if (s != null){
    s.close();
    s = null;
    }
    }
    catch (IOException io)
    {
    io.printStackTrace();
    }
    }

    }
    }
    }
      

  2.   


    import java.awt.*;
    import java.awt.event.*;
    import java.net.*;
    import java.io.*;
    import java.util.*;class Client {
    public static void main(String[] args) {
    MyFrame fram = new MyFrame();
    }
    }class MyFrame extends Frame {
    Panel p1;
    Panel p2;
    Panel p3;
    TextArea show;
    TextField input;
    Label port;
    Label ip;
    //Label name;
    TextField tPort;
    TextField tIp;
    //TextField tName;
    Button submit;
    Button login;
    Socket s = null;
    DataOutputStream  dos = null;
    DataInputStream dis = null;
    private boolean bConnect = false; public MyFrame() {
    init();
    }
    public void init() {
    /***********************************show begin****************************************************/
    p1 = new Panel();
    show = new TextArea(15,80);
    show.setEditable(false);
    p1.add(show);
    add(p1,BorderLayout.CENTER);
    /***********************************show end****************************************************/ /***********************************Panel2 begin****************************************************/
    p2 = new Panel(new FlowLayout(FlowLayout.LEFT));
    port = new Label("PORT");
    tPort  = new TextField(7);
    ip = new Label("IP");
    tIp = new TextField(25);
    //name = new Label("NAME");
    //tName = new TextField(7);
    login = new Button("login");
    p2.add(port);
    p2.add(tPort);
    p2.add(ip);
    p2.add(tIp);
    //p2.add(name);
    //p2.add(tName);
    p2.add(login);
    login.addActionListener(new submitAction());
    add(p2,BorderLayout.NORTH);
    /***********************************Panel2 end****************************************************/

    /***********************************Panel3 begin****************************************************/
    p3 = new Panel(new FlowLayout(FlowLayout.LEFT));
    input = new TextField(70);
    submit = new Button("submit");
    p3.add(input);
    p3.add(submit);
    input.addActionListener(new inputAction());
    submit.addActionListener(new submitAction());
    add(p3,BorderLayout.SOUTH);
    /***********************************Panel3 end****************************************************/
    /********************************fram begin************************************************/
    addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent e) {
    //disConnect();
    System.exit(0);
    }
    });
    this.setSize(300, 300);
    setLocation(100,100);
    pack();
    setVisible(true);
    /********************************fram end************************************************/
    }
    class submitAction implements ActionListener {
    public void actionPerformed(ActionEvent e) {
    if ("submit".equals(e.getActionCommand())) {
    String str = input.getText().trim();
    //show.setText(show.getText() + str + '\n');
    input.setText("");
    faSong(str);

    }else if ("login".equals(e.getActionCommand())) {
    connect();
    }
    }
    }
    /****************************************回车输出事件*****************************************************/
    class inputAction implements ActionListener {
    public void actionPerformed(ActionEvent e) {
    String str = input.getText().trim();
    //show.setText(show.getText() + str + '\n');
    input.setText("");
    faSong(str);
    }
    }
    /*******************************向服务端发送信息*******************************************************/
    public void faSong(String str)
    {
    try
    {
    dos.writeUTF(str);
    dos.flush();
    }
    catch (IOException e)
    {
    e.printStackTrace();
    }

    }
    /*******************************关闭资源*******************************************************/
    /*public void disConnect()
    {
    try
    {
    dos.close();
    }
    catch (IOException e)
    {
    //e.printStackTrace();
    System.out.println("系统错误");
    }

    }*/
    /*******************************************接收服务器发送的信息******************************************************/
    private class RecvClient implements Runnable
    {
    public void run()
    {
    try
    {
    while (bConnect)
    {
    String str = dis.readUTF();
    show.setText(show.getText()+str+'\n');
    }
    }
    catch (IOException e)
    {
    e.printStackTrace();
    }

    }
    }
    /*******************************connection begin*******************************************************/
    public void connect() {
    try
    {
    s = new Socket(tIp.getText(),Integer.parseInt(tPort.getText()));
    dos = new DataOutputStream(s.getOutputStream());
    dis = new DataInputStream(s.getInputStream());
    bConnect = true;
    new Thread(new RecvClient()).start();
    System.out.println("我连进来啦!~哈哈~~");
    }
    catch (UnknownHostException uhe)
    {
    uhe.printStackTrace();
    }
    catch (IOException e)
    {
    e.printStackTrace();
    }

    }
    }