Socket

来源:互联网 发布:vocaloid软件百度云 编辑:程序博客网 时间:2024/07/24 02:31
package ch3;import java.io.BufferedReader;import java.io.Flushable;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.OutputStream;import java.io.OutputStreamWriter;import java.net.InetAddress;import java.net.Socket;import java.net.UnknownHostException;public class ChatSocket {public static void main(String[] args) throws UnknownHostException, IOException {Socket socket=new Socket(InetAddress.getLocalHost(),9096);      OutputStreamWriter outputStreamWriter=new OutputStreamWriter(socket.getOutputStream());      BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(System.in));      BufferedReader reader=new BufferedReader(new InputStreamReader(socket.getInputStream()));      String line=null;      String line1 =null;      while((line=bufferedReader.readLine())!=null){      outputStreamWriter.write(line+"\r\n");      outputStreamWriter.flush();     line1=reader.readLine();    System.out.println("服务端给我发送的信息是"+line1);      }            socket.close();      }}

0 0
原创粉丝点击