客户端向服务器发送数据
import java.io.IOException; import java.io.InputStream; import java.net.ServerSocket; import java.net.Socket; //服务端 public class ServerTCP { public static void main(String[] args) throws IOException { System.out.println("服务器启动,等待连接......."); //1、创建ServerSocket对象,绑定端口,开始等待连接 ServerSocket ss = new ServerSocket(9999); //2、接收连接accept方法,返回socket(客户端)对象 Socket accept = ss.accept(); //3、通过socket获取输入流 InputStream is = accept.getInputStream(); //4、一次性读取数据 //4.1创建字节数组 byte[] b = new byte[1024]; //4.2读数据到字节数组中 int read = is.read(b); //4.3解析数组,打印字符串信息 String s = new String(b, 0, read); System.out.println(s); //5、关闭资源 is.close(); accept.close(); } }
import java.io.IOException; import java.io.OutputStream; import java.net.Socket; //客户端 public class ClientTCP { public static void main(String[] args) throws IOException { System.out.println("客户端发送数据"); //1、创建Socket(ip,port),确定发送到哪里 Socket client = new Socket("10.31.151.54", 9999); //2获取流对象,输出流 OutputStream ops = client.getOutputStream(); //3、写出数据 ops.write("你好,服务器".getBytes()); //4、关闭资源 ops.close(); client.close(); } }
服务器向客户端回写数据
import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.ServerSocket; import java.net.Socket; //服务端实现 public class ServerTCP { public static void main(String[] args) throws IOException { System.out.println("服务端正在启动,等待连接......"); //1、创建ServerSocker对象,绑定端口,开始等待连接 ServerSocket ss = new ServerSocket(8888); //2、接收连接accept方法,返回socket对象 Socket server = ss.accept(); //3、通过socket获取输入流 InputStream is = server.getInputStream(); //4、一次行读取数据 //4.1、创建字节数组 byte[] b = new byte[1024]; //4.2、读取数据到字节数组中 int len = is.read(b); //4.3解析数组,打印字符串信息 String s = new String(b, 0, len); System.out.println(s); //============回写数据=========== //5、通过socket获取输出流 OutputStream os = server.getOutputStream(); //6、回写数据 os.write("我很好,谢谢你".getBytes()); //7、关闭资源 ss.close(); is.close(); os.close(); } }
import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.Socket; //客户端实现 public class ClientTCP { public static void main(String[] args) throws IOException { //1、创建socket(ip,port),确定连接到哪里 Socket client = new Socket("10.31.151.54", 8888); //2、通过Socket获取输出流 OutputStream os = client.getOutputStream(); //3、写出数据 os.write("你好吗?服务器,我来了".getBytes()); //==================解析回写================== //4、通过socket获取输入流对象 InputStream is = client.getInputStream(); //5、读取数据 byte[] b = new byte[1024]; //6、把数据读取到数组中 int len = is.read(b); //7、字节数组解析 System.out.println(new String(b,0,len)); //8、关闭资源 client.close(); os.close(); is.close(); } }
客户端和服务器相互循环输入数据进行传输
import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.ServerSocket; import java.net.Socket; import java.util.Scanner; //服务器 public class Server { public static void main(String[] args) throws IOException { ServerSocket ss = new ServerSocket(8899); Socket server = ss.accept(); InputStream is = server.getInputStream(); OutputStream os = server.getOutputStream(); Scanner sc = new Scanner(System.in); while (1<2){ //读 byte[] b = new byte[1024]; int len = is.read(b); String s = new String(b, 0, len); System.out.println(s); //写 os.write(sc.next().getBytes()); } } }
import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.Socket; import java.util.Scanner; //客户端 public class ClientTCP { public static void main(String[] args) throws IOException { System.out.println("客户端发送数据"); //1、创建socket(ip,port)对象,确定连接到哪里 Socket client = new Socket("10.31.151.54", 8899); //2、获取输出流对象 OutputStream os = client.getOutputStream(); InputStream is = client.getInputStream(); Scanner sc = new Scanner(System.in); //3、客户端先写再读 while (1<2){ //写 os.write(sc.next().getBytes()); //读 byte[] b = new byte[1024]; int len = is.read(b); String s = new String(b, 0, len); System.out.println(s); } } }
使用多线程进行传递
以上发送信息存在问题:在客户端连接服务器后,只能按顺序你发完我发。无法达到聊天效果。抢占式聊天,谁打 字快谁先传输
import java.io.IOException; import java.io.InputStream; //接受消息线程 //多线程,需要继承Thread() public class Rece extends Thread { InputStream is ; //实现run方法 @Override public void run() { String msg = null; while (1<2){ byte[] b = new byte[1024]; int len; try { len = is.read(b); msg = new String(b,0,len); System.out.println(msg); } catch (IOException e) { // e.printStackTrace(); System.exit(0); } } } }
import java.io.IOException; import java.io.OutputStream; import java.util.Scanner; //发送线程 public class Send extends Thread { OutputStream os; @Override public void run() { Scanner sc = new Scanner(System.in); while (1<2){ String msg = sc.nextLine(); try { os.write(msg.getBytes()); } catch (IOException e) { //e.printStackTrace(); System.exit(0); } } } }
import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.ServerSocket; import java.net.Socket; //服务端 public class Server { public static void main(String[] args) throws IOException { ServerSocket ss = new ServerSocket(8877); Socket server = ss.accept(); InputStream is = server.getInputStream(); OutputStream os = server.getOutputStream(); Send st = new Send(); Rece rt = new Rece(); st.os=os; rt.is = is; st.start(); rt.start(); } }
import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.Socket; import java.util.Scanner; //客户端(先写后读) public class Client { public static void main(String[] args) throws IOException { Socket client = new Socket("10.31.151.54", 8877); OutputStream os = client.getOutputStream(); InputStream is = client.getInputStream(); Scanner sc = new Scanner(System.in); Send st = new Send(); Rece rt = new Rece(); st.os = os; rt.is= is; st.start(); rt.start(); } }