用socket进行通信是服务器与客户端的互发信息

《此代码是总结出点简单的socket通讯的应用》

import java.io.BufferedReader;

  1. List item

import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.DataOutputStream;
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.ServerSocket;
import java.net.Socket;

public class ServerSocketDemo {
*/

  • 1.创建服务器端serversocker对象,指明服务器端口
  • 2.接受客户端的请求,创建socket对象
  • 3.获取socket的输入流
  • 4.读取数据
  • 5.关闭输入流
  • /**
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    try {
    ServerSocket ss=new ServerSocket(8080);
    System.out.println(“服务器启动”);
    Socket socket=ss.accept();
    System.out.println(“服务器已建立链接”);
    //创建输入流,读取客户端发来的信息
    InputStream in=socket.getInputStream();
    OutputStream out=socket.getOutputStream();

/* BufferedReader reader=new BufferedReader(new InputStreamReader(in,“utf-8”));
String msg1=reader.readLine(); System.out.println(“客户端发送的信息”+msg1);
*/

DataInputStream dis=new DataInputStream(in);
//DataInputStream dis=new DataInputStream(socket.getInputStream());
String echo=dis.readUTF();
System.out.println("客户端发送的信息"+echo);

 
  
 //向客户端发送信息
 String msg="no hello wold";
 
/*
  PrintWriter writer= new PrintWriter(new BufferedWriter(new OutputStreamWriter(out,"utf-8")));
  writer.println(msg);
  writer.flush();
 */
  /* writer.close();//
  reader.close();
in.close();*/

 DataOutputStream dos=new DataOutputStream(out);
 dos.writeUTF(msg);
 dos.flush();

// out.flush();
// out.close();

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}

  1. List item
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.DataInputStream;
    import java.io.DataOutputStream;
    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 ClientSocketDemo {
/**

  • 1.创建客户端Socket对象,指明服务器的地址和端口号

  • 2.获取Socket的输出流

  • 3.写出数据

  • 4.关闭输出流

  • 要点::DataOutputStream数据输出流允许应用程序以适当方式将基本 Java 数据类型写入输出流中。然后,应用程序可以使用数据输入流将数据读入。

  • @author jf

  • */
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    try {
    //向服务器发送信息
    //1.创建客户端Socket对象,指明服务器的地址和端口号
    //Socket(InetAddress address, int port)

    Socket socket=new Socket(“127.0.0.1”,8080);
    String msg=“hello world”;
    //2.获取Socket的输出流
    OutputStream out=socket.getOutputStream();
    InputStream in=socket.getInputStream();

    //3.写出数据 OutputStreamWriter(OutputStream out, Charset cs(格式))
    /PrintWriter writer= new PrintWriter(new BufferedWriter(new OutputStreamWriter(out,“utf-8”)));
    writer.println(msg);
    writer.flush();
    /

    DataOutputStream dos=new DataOutputStream(out);
    dos.writeUTF(msg);// 以与机器无关方式使用 UTF-8 修改版编码将一个字符串写入基础输出流。
    dos.flush();

    //接收服务器发送的信息
    /* BufferedReader reader=new BufferedReader(new InputStreamReader(in,“utf-8”));
    String msg1=reader.readLine();//阻塞式方法
    System.out.println(“服务器发送的信息是:”+msg1);/
    //4.关闭输出流
    /
    reader.close();*/

    DataInputStream dis=new DataInputStream(in);
    String echo=dis.readUTF();
    System.out.println(“服务器发送的信息是:”+echo);
    dis.close();
    dos.close();

// in.close();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}
先运行服务端再运行客户端~
还可以进行控制台输入
BufferedReader console=new BufferedReader(new InputStreamReader(System.in));
String info=console.readLine();
dos.writerUTF(info);

猜你喜欢

转载自blog.csdn.net/qq_43615815/article/details/83796098
0条评论
添加一条新回复