1:一个客户端对一个服务端发一个消息
个人写的代码
public class Client {
public static void main(String[] args) {
try {
/*创建一个流套接字并将其连接到指定 IP 地址的指定端口号。*/
Socket s = new Socket("127.0.0.1",3305);//设置客户端的ip地址和端口号
Scanner sc = new Scanner(System.in);//创建键盘输入对象
System.out.println("请输入:");
String s1 = sc.next();
PrintStream s2 = new PrintStream(s.getOutputStream());//创建打印输出流
byte[] b = s1.getBytes();
s2.write(b);
s2.close();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public class Servlet {
public static void main(String[] args) {
try {
//创建绑定到特定端口的服务器套接字。
ServerSocket s = new ServerSocket(3305);
//侦听并接受到此套接字的连接。
Socket ss = s.accept();
System.out.println(s+"创建了");
System.out.println("客户端"+ss+"上线了");
BufferedReader s1 = new BufferedReader(new InputStreamReader(ss.getInputStream()));
String s2 = s1.readLine();
System.out.println("客户说:"+s2);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
步骤:
1:先运行服务器的类Servlet
2:之后运行客户端Client
3:在客户端类输入123
4:查看服务器类的运行结果
最后一行结果显示读取的客户端给服务器发来的信息
老师写的代码(最正规的)
public class Server {
public static void main(String[] args) throws Exception {
ServerSocket server=new ServerSocket(8888);
Socket client=null;
System.out.println("服务器已开启");
client=server.accept();
BufferedReader reader=new BufferedReader(new InputStreamReader
(client.getInputStream()));
String info=reader.readLine();
System.out.println(info);
}
}
public class Client {
public static void main(String[] args)throws Exception {
Socket socket=new Socket("127.0.0.1",8888);
//向服务器发送一条信息
BufferedWriter out=new BufferedWriter(new OutputStreamWriter
(socket.getOutputStream()));
//PrintStream或者PrintWriter println()方法
out.write("hello Server\r\n");//?
out.flush();
}}
2:多个客户端给一个服务器发送消息
public class Client extends Thread {
public static void main(String[] args) {
Socket s = null;
try {
s = new Socket("127.0.0.1",3302);
new ClientThread(s).start();//调用创建的ClientThread类并且启动线程
PrintStream ss = new PrintStream(s.getOutputStream());
Scanner sc=new Scanner(System.in);
System.out.println("请输入:");
while(true){
String s2 = sc.next();
ss.print(s2+"\r\n");
ss.flush();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public class ClientThread extends Thread {
private static Socket s;
public ClientThread(Socket s){
this.s = s;
}
public void run(){
try {
BufferedReader ss = new BufferedReader(new InputStreamReader(s.getInputStream()));
System.out.println("服务端回应"+ss.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
}
public class Servlet {
public Servlet(){
ServerSocket ss =null;
try {
ss = new ServerSocket(3302);
System.out.println(ss+"创建了");
boolean flag = true;
Socket s=null;
while(flag){//遍历创建的所有客户端
s= ss.accept();
System.out.println("客户端"+s+"上线了");
new ServletManger(s).start();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
new Servlet();
}
}
public class ServletManger extends Thread {
private static Socket s;
public ServletManger(Socket s) {
this.s = s;
}
public void run(){
try {
BufferedReader s1 = new BufferedReader(new InputStreamReader(s.getInputStream()));
System.out.println(s1.readLine());
PrintStream s2 = new PrintStream(s.getOutputStream());
while(true){
System.out.println(s+"说:\n"+s1.readLine()+"\t");
s2.println("hello");
s2.flush();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
步骤:
1:先运行服务器的类Servlet
2:之后两次运行客户端Client(相当于启动了两个客户端的线程)
3:在客户端1的控制台输入123,再客户端1的控制台输入321
4:查看服务器类的运行结果是否都有两个客户端发来的信息
结果显示启动了两条客户端线程,并在服务端的控制台也显示了两条来自于两个客户端输入的信息
3:两个客户端通过服务端互相发送消息
public class Client extends Thread {
public Client(){//内部类 最大的优点是调用外部类任何对象
try {
Socket s = new Socket("127.0.0.1",3302);
new ClientThread(s).start();//调用集成线程的类来启动客户端的线程
PrintStream ss = new PrintStream(s.getOutputStream());
Scanner sc=new Scanner(System.in);
System.out.println("请输入:");
while(true){
String s2 = sc.next();
ss.print(s2+"\r\n");
ss.flush();
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new Client();//调用该类的构造方法
}
}
public class ClientThread extends Thread {
private Socket s;
public ClientThread(Socket s){
this.s = s;
}
public void run(){
try {
while(true){//不断接收来自于服务端的消息(该消息是另一个客户端发给服务端的,是要把别的客户端发的信息群发给当前未收到消息的客户端)
BufferedReader ss = new BufferedReader(new InputStreamReader(s.getInputStream()));
System.out.println(s+"消息"+ss.readLine());
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public class Servlet {
public Servlet(){//内部类
ServerSocket ss =null;
Set<Socket> set = null;
set = new HashSet<Socket>();//创建一个包含客户端套接字的集合
try {
ss = new ServerSocket(3302);
System.out.println(ss+"创建了");
boolean flag = true;
Socket s=null;
while(flag){
s= ss.accept();
set.add(s);将创建的每一个客户端线程添加到集合中
System.out.println("客户端"+s+"上线了");
new ServletManger(s,set).start();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
new Servlet();
}
}
public class ServletManger extends Thread {
private Socket s;
private Set<Socket> set;
public ServletManger(Socket s,Set<Socket> set) {
this.s = s;
this.set = set;
}
public void run(){
try {
BufferedReader s1 = new BufferedReader(new InputStreamReader(s.getInputStream()));
PrintStream s2 = null;
while(true){
String s5 = s1.readLine();//将客户端发送的消息都发给变量s5
/*System.out.println(s+"说:\n"+s1.readLine()+"\t");*/ //不可以写成此代码,会让服务器初一一直等待状态,无法全部获取客户端发来的消息
for(Socket s4:set){//遍历集合中的所创建的所有客户端线程对象
s2 = new PrintStream(s4.getOutputStream());
s2.println(s5);
s2.flush();
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
步骤:
1:先运行服务器的类Servlet
2:之后两次运行客户端Client(相当于启动了两个客户端的线程)
3:在客户端1的控制台输入122222,再客户端1的控制台输入31111111
4:查看服务器类的运行结果和两个客户端是否能接收到对方的消息