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

需求:客户端给服务端发送数据,服务端收到后,给客户端反馈信息。

         步骤:客户端:1、建立socket服务,指定要连接主机和端口;2、获取socket流中的输出流,将数据写到该流中,通过网络发送给服务端,3、获取socket流中的输入流,将服务端反馈的数据获取到,并打印,4关闭客户端资源。服务端:1、建立ServerSocket服务,指定监听端口;2、获取客户端的socket;3、获取客户端socket的输入流,得到客户端发来的数据,再获得客户端socket的输出流,将反馈数据发出;4、关闭客户端和服务端。

        代码如下:
import java.net.*;
import java.io.*;
class TCPclient2{
        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);
                }
                BufferedWriter bw=null;
                BufferedReader br=null;
                try{
                        bw=new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
                }catch(IOException e3){
                        throw new RuntimeException(e3);
                }
                try{
                        br=new BufferedReader(new InputStreamReader(s.getInputStream()));
                }catch(IOException e4){
                        throw new RuntimeException(e4);
                }
                try{
                        bw.write("TCP客户端来了!");
                        bw.newLine();
                        bw.flush();
                        s.shutdownOutput();
                }catch(IOException e5){
                        throw new RuntimeException(e5);
                }
                String buff=null;
                try{       
                        while((buff=br.readLine())!=null)
                                System.out.println(buff);
                }catch(IOException e6){
                        throw new RuntimeException(e6);
                }
                try{
                     s.close();
                }catch(IOException e7){
                     throw new RuntimeException(e7);
                }
        }
}
import java.net.*;
import java.io.*;
class TCPserver2{
        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()+"......"+"已连接");
                BufferedReader br=null;
                BufferedWriter bw=null;
                try{
                        br=new BufferedReader(new InputStreamReader(s.getInputStream()));
                }catch(IOException e4){
                     throw new RuntimeException(e4);
                }
                try{
                        bw=new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
                }catch(IOException e5){
                     throw new RuntimeException(e5);
                }
                String buff=null;
                try{
                        while((buff=br.readLine())!=null)
                                       System.out.println("ip"+s.getLocalAddress().getHostAddress()+"......"+buff);
                }catch(IOException e5){
                        throw new RuntimeException(e5);
                }
                try{
                        bw.write("服务器已连接");
                        bw.newLine();
                        bw.flush();
                }catch(IOException e6){
                        System.out.println("服务器写异常");
                        throw new RuntimeException(e6);
                }
                try{
                     s.close();
                }catch(IOException e7){
                     throw new RuntimeException(e7);
                }
                try{
                     ss.close();
                }catch(IOException e8){
                     throw new RuntimeException(e8);
                }
        }
}
您需要登录后才可以回帖 登录 | 加入黑马