【java】网络socket编程简单示例

时间:2022-03-16 10:34:53
 package 网络编程;

 import java.io.IOException;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket; public class TestServerSocket {
public static void main(String[] args) throws IOException {
ServerSocket server=new ServerSocket(9999);
System.out.println("正在连接中……");
Socket client=server.accept();
PrintStream out=new PrintStream(client.getOutputStream());
out.println("北京欢迎你。");
out.close();
client.close();
server.close();
}
}

Server

 package 网络编程;

 import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner; public class TestSocket {
public static void main(String[] args) throws UnknownHostException, IOException {
Socket client=new Socket("localhost",9999);
Scanner scan=new Scanner(client.getInputStream());
scan.useDelimiter("\n");
if(scan.hasNext())
System.out.println("响应数据:"+scan.next());
scan.close();
client.close();
}
}

Client


echo模型:

 package 网络编程;

 import java.io.IOException;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner; public class TestEchoServer {
public static void main(String[] args) throws IOException {
ServerSocket server = new ServerSocket(9999);
Socket client = server.accept();
Scanner scan = new Scanner(client.getInputStream());// 接收客户端输入
PrintStream out = new PrintStream(client.getOutputStream());// 输出到客户端
boolean flag = true;
String tmp = "";
while (flag) {
if (scan.hasNext()) {
tmp = scan.next().trim();
if (tmp.equalsIgnoreCase("bye")) {
out.println("再见!");
flag = false;
} else {
out.println("Echo:" + tmp);
}
}
}
out.close();
scan.close();
client.close();
server.close();
}
}

EchoServer

 package 网络编程;

 import java.io.IOException;
import java.io.PrintStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner; public class TestEchoClient {
public static void main(String[] args) throws UnknownHostException, IOException {
Socket client=new Socket("localhost",9999);
Scanner scan=new Scanner(System.in);//键盘输入
Scanner in=new Scanner(client.getInputStream());//服务端输入
PrintStream out=new PrintStream(client.getOutputStream());//输出到服务端
scan.useDelimiter("\n");
// in.useDelimiter("\n");
boolean flag=true;
String msg="";
while(flag){
if(scan.hasNext()){//等待用户从键盘输入
msg=scan.next().trim();
out.println(msg);//信息转给服务端
if(msg.equalsIgnoreCase("bye"))
flag=false;
else
System.out.println(in.next());//打印服务端响应的信息
}
}
out.close();
in.close();
scan.close();
client.close();
}
}

EchoClient

 package 网络编程;

 import java.io.IOException;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner; class ClientThread implements Runnable {
private Socket client; public ClientThread(Socket client) {
this.client = client;
} @Override
public void run() {
try {
Scanner scan = new Scanner(client.getInputStream());// 接收客户端输入
PrintStream out = new PrintStream(client.getOutputStream());// 输出到客户端
boolean flag = true;
String tmp = "";
while (flag) {
if (scan.hasNext()) {
tmp = scan.next().trim();
if (tmp.equalsIgnoreCase("bye")) {
out.println("再见!");
flag = false;
} else {
out.println("Echo:" + tmp);
}
}
}
out.close();
scan.close();
client.close();
} catch (Exception e) {
e.printStackTrace();
}
}
} public class TestEchoServer {
public static void main(String[] args) throws IOException {
ServerSocket server = new ServerSocket(9999);
boolean flag = true;
while (flag) {
Socket client = server.accept();//连接多个客户端
new Thread(new ClientThread(client)).start();//每连接一个客户端启动一个线程
}
server.close();
}
}

EchoServer多线程