LandryBean 2019-11-02
import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; //写服务器端 public class TestServerData { public static void main(String[] args) { try { //1.创建 ServerSocket 类型的对象, 并提供端口号 ServerSocket ss=new ServerSocket (8888); //2.等待客户端的连接请求,使用accept()方法,保持阻塞状态 System.out.print("等待客户端的连接请求...."); Socket s = ss.accept(); System.out.println("客户端连接成功!"); //3.使用 输入输出流 进行通信 //服务器接收客户端 发来的 数据 66,并打印出来 DataInputStream dis=new DataInputStream(s.getInputStream()); int res = dis.readInt(); System.out.println("res= "+res); //关闭相关的套接字 s.close(); ss.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
import java.io.DataOutputStream; import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; public class TestClientData { public static void main(String[] args) { try { //1. 创建 Socket 类型的对象, 指定IP地址和 端口号 Socket s = new Socket ("183.16.232.251",8888); //2.使用输入输出流 进行通信 //让客户端向服务器端发送整型数据 66 DataOutputStream dos=new DataOutputStream(s.getOutputStream()); dos.writeInt(66); System.out.println("成功发送数据至服务器!"); //3.关闭Socket对象 s.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.ServerSocket; import java.net.Socket; public class TestServerString { public static void main(String[] args) { try { //1.创建ServerSocket类型的对象 ,并提供端口号 ServerSocket ss = new ServerSocket(8889); //2.等待客户端的连接请求,使用accept()方法; Socket s = ss.accept(); System.out.println("客户端连接成功!"); //3.使用 输入输出流进行通信; //服务器接收来自客户端 发来的“hello”,并打印 BufferedReader br = new BufferedReader( new InputStreamReader(s.getInputStream())); String str = br.readLine(); System.out.println("str= "+str); //向客户端回发消息 "I received!!! " //4.关闭相关的套接字 br.close(); s.close(); ss.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
import java.io.IOException; import java.io.PrintStream; import java.net.Socket; public class TestClientString { public static void main(String[] args) { try { //1.创建Socket类的对象,提供IP号,并且指定 端口号 Socket s=new Socket("183.16.232.251",8889); //2.使用 输入输出流 进行通信 //向服务器端发送 字符串数据 “hello” PrintStream ps=new PrintStream (s.getOutputStream()); ps.println("hello"); System.out.println("数据成功发送到服务器!"); //等待接收 服务器的 回复 ,并且打印回复的结果 //3.关闭Socket对象 ps.close(); s.close(); } catch (IOException e) { e.printStackTrace(); } } }
import java.io.BufferedReader; import java.io.DataInputStream; import java.io.InputStreamReader; import java.io.PrintStream; import java.net.ServerSocket; import java.net.Socket; public class TestServerString { public static void main(String[] args) { try{ //1.创建ServerSocket类型的对象,并提供端口号 ServerSocket ss = new ServerSocket(8888); //2.等待客户端的连接请求,使用accept()方法,保持阻塞状态 System.out.println("等待客户端的连接请求..."); Socket s = ss.accept(); System.out.println("客户端连接成功!"); //3.使用输入输出流进行通信 /* //服务器接收客户端发来的"hello"并打印 BufferedReader br = new BufferedReader( new InputStreamReader(s.getInputStream())); String str = br.readLine(); System.out.println(str); // hello //向客户端回发消息“I received!” PrintStream ps = new PrintStream(s.getOutputStream()); ps.println("I received!"); */ BufferedReader br = new BufferedReader( new InputStreamReader(s.getInputStream())); PrintStream ps = new PrintStream(s.getOutputStream()); //编程实现服务器可以不断地客户端进行通信 while(true){ //服务器接收客户端发来的消息并打印 String str = br.readLine(); //当客户端发来"bye"时,结束循环 if("bye".equalsIgnoreCase(str)) break; System.out.println("张三说:" + str); //向客户端回发消息“I received!” ps.println("I received!"); } //4.关闭相关的套接字 ps.close(); br.close(); s.close(); ss.close(); }catch(Exception e){ e.printStackTrace(); } } }
import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.InputStreamReader; import java.io.PrintStream; import java.net.Socket; public class TestClientString { public static void main(String[] args) { try{ //1.创建Socket类型的对象,并指定IP地址和端口号 Socket s = new Socket("192.168.99.234", 8888); //2.使用输入输出流进行通信 /* //提示用户输入要发送的内容 BufferedReader br = new BufferedReader( new InputStreamReader(System.in)); System.out.println("请输入要发送的内容:"); String msg = "张三说:" + br.readLine(); //让客户端向服务器发送数据"hello" PrintStream ps = new PrintStream(s.getOutputStream()); //ps.println("hello"); ps.println(msg); System.out.println("成功发送数据到服务器!"); //等待接收服务器的回复,并打印回复的结果 BufferedReader br2 = new BufferedReader( new InputStreamReader(s.getInputStream())); String str2 = br2.readLine(); System.out.println("服务器发来的消息是:" + str2); */ BufferedReader br = new BufferedReader( new InputStreamReader(System.in)); PrintStream ps = new PrintStream(s.getOutputStream()); BufferedReader br2 = new BufferedReader( new InputStreamReader(s.getInputStream())); //编程实现客户端不断地和服务器进行通信 while(true){ //提示用户输入要发送的内容 System.out.println("请输入要发送的内容:"); String msg = br.readLine(); ps.println(msg); //当客户端发送"bye"时,结束循环 if("bye".equalsIgnoreCase(msg)) break; //等待接收服务器的回复,并打印回复的结果 String str2 = br2.readLine(); System.out.println("服务器发来的消息是:" + str2); } //3.关闭Socket对象 br2.close(); br.close(); ps.close(); s.close(); }catch(Exception e){ e.printStackTrace(); } } }