A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

  思路:1、建立客户端点和服务端点;2、 读取客户端已有的图片数据;3、通过socket输出流将数据发给服务端;4、读取服务端反馈信息;5、关闭资源。
代码如下
import java.net.*;
import java.io.*;
class TCPclient4{
        public static void main(String[] args){
                Socket s=null;
                try{
                        s=new Socket(InetAddress.getLocalHost(),18888);
                }catch(UnknownHostException e1){
                        throw new RuntimeException(e1);
                }catch(IOException e2){
                        throw new RuntimeException(e2);
                }
                BufferedReader br=null;
                try{
                        br=new BufferedReader(new InputStreamReader(s.getInputStream()));
                }catch(IOException e3){
                        throw new RuntimeException(e3);
                }
                OutputStream os=null;
                try{
                        os=s.getOutputStream();
                }catch(IOException e4){
                        throw new RuntimeException(e4);
                }
                FileInputStream fis=null;
                try{
                        fis=new FileInputStream("TCPpic.jpg");
                }catch(IOException e5){
                        throw new RuntimeException(e5);
                }
                byte[] buff=new byte[1024];
                int len=0;
                try{
                      while((len=fis.read(buff))!=-1){
                          os.write(buff,0,len);
                          os.flush();
                      }
                      s.shutdownOutput();
                }catch(IOException e6){  
                        throw new RuntimeException(e6);
                }
                try{
                        System.out.println(br.readLine());
                }catch(IOException e7){
                        throw new RuntimeException(e7);
                }
                try{
                     s.close();
                }catch(IOException e8){
                     throw new RuntimeException(e8);
                }
        }
}
import java.net.*;
import java.io.*;
class TCPserver4{
        public static void main(String[] args){
                ServerSocket ss=null;
                Socket s=null;
                try{
                        ss=new ServerSocket(18888);
                }catch(SocketException e1){
                        throw new RuntimeException(e1);
                }catch(IOException e2){
                        throw new RuntimeException(e2);
                }
                try{
                        s=ss.accept();
                }catch(IOException e3){
                        throw new RuntimeException(e3);
                }
                System.out.println("ip"+s.getLocalAddress().getHostAddress()+"......"+"已连接");
                InputStream is=null;
                BufferedWriter bw=null;
                FileOutputStream fos=null;
                try{
                        is=s.getInputStream();
                }catch(IOException e4){
                     throw new RuntimeException(e4);
                }
                try{
                        bw=new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
                }catch(IOException e5){
                     throw new RuntimeException(e5);
                }
                try{
                        fos=new FileOutputStream("TCPpic_copy.jpg");
                }catch(IOException e6){
                     throw new RuntimeException(e6);
                }
                byte[] buff=new byte[1024];
                int len=0;
                try{
                        while((len=is.read(buff))!=-1){
                                fos.write(buff,0,len);
                                fos.flush();
                        }               
                }catch(IOException e5){
                        throw new RuntimeException(e5);
                }
                try{
                        bw.write("上传成功!");
                        bw.newLine();
                        bw.flush();
                }catch(IOException e6){
                        throw new RuntimeException(e6);
                }
                try{
                     s.close();
                }catch(IOException e7){
                     throw new RuntimeException(e7);
                }
                try{
                     ss.close();
                }catch(IOException e8){
                     throw new RuntimeException(e8);
                }
        }
}
这个服务端有个局限性,当A客户端连接上以后,被服务端获取到,服务端执行具体流程,这时B客户端连接,只有等待,因为服务端还没有处理完A客户端的请求,如果通过循环,还没有循环回来执行下一次accept()方法,所以暂时获取不到B客户端对象。那么为了可以让多个客户端同时并发访问服务端,服务端最好就是将每个客户端封装到一个单独的线程中,这样就可以同时处理多个客户端请求。

只要明确了每一个客户端要在服务端执行的即可,就将该代码存入run()方法中。

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马