问答详情
源自:5-1 Socket 总结

能不给我图片和文件TCP传输的代码呢

RTTTTTTTTTTTTTTTTTTTTTTTTTTTTT

提问者:不看6324不混吃等死 2017-03-16 13:00

个回答

  • qq_小树_6
    2017-03-26 07:21:26

    package java_socket;


    import java.io.BufferedReader;

    import java.io.IOException;

    import java.io.InputStream;

    import java.io.InputStreamReader;

    import java.io.OutputStream;

    import java.io.OutputStreamWriter;

    import java.io.PrintWriter;

    import java.net.Socket;

    import java.net.UnknownHostException;


    public class client {


    public static void main(String[] args)  {

    //1.创建服务器地址和端口号

    try {

    Socket client1 = new Socket("localhost",8888);

    OutputStream os1=client1.getOutputStream();

    PrintWriter pw1=new PrintWriter(os1);

    pw1.write("我是客户端2");

    pw1.flush();

    client1.shutdownOutput();

    InputStream is1=client1.getInputStream();

    InputStreamReader isr=new InputStreamReader(is1);

    BufferedReader br=new BufferedReader(isr);

    String gain=null;

    while((gain=br.readLine())!=null){

    System.out.println("我是客户端,我从服务器接收的数据"+gain);

    }

    client1.shutdownInput();

    br.close();

    isr.close();

    is1.close();

    pw1.close();

    os1.close();

    client1.close();

    } catch (IOException e) {

    // TODO Auto-generated catch block

    e.printStackTrace();

    }

    }


    }

    package java_socket;


    import java.io.BufferedInputStream;

    import java.io.BufferedReader;

    import java.io.FileInputStream;

    import java.io.IOException;

    import java.io.InputStream;

    import java.io.InputStreamReader;

    import java.io.OutputStream;

    import java.io.PrintWriter;

    import java.net.InetAddress;

    import java.net.ServerSocket;

    import java.net.Socket;


    public class server {


    public static void main(String[] args) {

    try {

    //1.创建socket对象

    ServerSocket  serversocket=new ServerSocket(8888);

    //2.调用accept()方法,等待客户端的连接

    System.out.println("服务器即将启动");

    // 计数器记录有几个客户端进行连接

    int count=0;

    Socket socket=null;

    while(true){

    socket=serversocket.accept();

    // 启动一个线程

    socketThread st1=new socketThread(socket);

    st1.start();

    count++;

    System.out.println("我是服务器,客户端连接 数量为"+count);

    InetAddress ia=socket.getInetAddress();

    System.out.println("当前客户端的IP为"+ia.getHostAddress());

    }

    //3.获取输入流进行信息交互

    // InputStream is1=socket.getInputStream();

    // InputStreamReader isr1=new InputStreamReader(is1);

    // BufferedReader bis1=new BufferedReader(isr1);

    // String info =null;

    // while((info=bis1.readLine())!=null){

    // System.out.println("我是服务器,客户端说   "+info);

    //

    //

    // }

    // socket.shutdownInput();

    // OutputStream os1=socket.getOutputStream();

    // PrintWriter pw1=new PrintWriter(os1);

    // pw1.write("我是服务器,我说登录成功");

    // pw1.flush();

    // socket.shutdownOutput();

    // pw1.close();

    // os1.close();

    // bis1.close();

    // isr1.close();

    // is1.close();

    // socket.close();

    // serversocket.close();

    } catch (IOException e) {

    // TODO Auto-generated catch block

    e.printStackTrace();

    }


    }


    }