原理:
1.服务器先实例化一个ServerSocket对象,表明通信将在某个端口号,这样就服务器开始侦听客户端的消息;
2.服务器调用ServerSocket类的accept()方法.开始侦听客户端的连接请求。
3服务器在等待后, 一个客户端实例化一个Socket对象,指定服务器名称和连接的端口号;
4.如果通信建立,客户端现在就拥有了一个能和服务器通信的Socket对象
5.在服务器端,accept()方法子服务器上返回一个连接到客户端套接字的新的套接字。
在连接建立后,双方就可建立起通信信道进行对话了。最后通信结束后记得关闭通信信道
服务器的IP地址为127.0.0.1
端口号为11111或22222
服务器端:
package com.mec.simpile.listen;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
private ServerSocket server;
private DataInputStream dis;
private DataOutputStream dos;
private boolean goon;
public Server(){
}
public void start(){
try {
System.out.println("开始建立服务器");
server = new ServerSocket(11111);
System.out.println("-------------------------");
System.out.println("开始侦听客户端消息");
System.out.println("-------------------------");
Socket client = server.accept();
String clientip = client.getInetAddress().getHostAddress();
System.out.println("侦听客户端"+clientip+"的链接请求");
coversation(client,clientip) ;
} catch (Exception e) {
e.printStackTrace();
}
}
public void coversation(Socket client,String clientip) throws IOException{
dis = new DataInputStream(client.getInputStream());
dos = new DataOutputStream(client.getOutputStream());
String mess = dis.readUTF();
System.out.println("-----------");
System.out.println("收到"+clientip+"的"+mess);
System.out.println("-------------------------");
dos.writeUTF("收到了:"+mess);//中
if(mess.equals("bye")){
dis.close();
dos.close();
client.close();
}
}
public static void main(String[] args) throws IOException {
Server server = new Server();
server.start();
}
}
客户端:
package com.mec.simpile.listen;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.InetAddress;
import java.net.Socket;
public class Client {
private Socket socket;
private DataInputStream dis;
private DataOutputStream dos;
public Client(){
try {
socket = new Socket("127.0.0.1", 11111);
dis = new DataInputStream(socket.getInputStream());
dos = new DataOutputStream(socket.getOutputStream());
dos.writeUTF("我来了");
String mess = dis.readUTF();
System.out.println("********************");
System.out.println("来自服务器的消息:"+mess);
System.out.println("********************");
dis.close();
dos.close();
socket.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new Client();
}
}
最终运行的结果为
服务器端:
开始建立服务器
-------------------------
开始侦听客户端消息
-------------------------
侦听客户端127.0.0.1的链接请求
-----------
收到127.0.0.1的我来了
-------------------------
客户端:
********************
来自服务器的消息:收到了:我来了
********************
//
以上是简单的一发一接模式,下面就是一对多模式,一个服务器可以连接多个客户端:
客户端:
package com.mec.complex.listener;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.Scanner;
public class Client {
private Socket socket;
private DataInputStream dis;
private DataOutputStream dos;
private Scanner scanner;
public Client (){
}
public void start() throws IOException{
try {
socket = new Socket("192.168.1.82", 22222);
dis = new DataInputStream(socket.getInputStream());
dos = new DataOutputStream(socket.getOutputStream());
System.out.println("输入bye时截至通话,请输入");
scanner = new Scanner(System.in);
// dos.writeUTF("bHJ");
while(scanner.hasNext()){
String mess = scanner.nextLine();
dos.writeUTF(mess);
mess = dis.readUTF();
System.out.println("收到服务器端消息:"+mess);
if(mess.equals("bye")){
System.out.println("服务端正常下线");
break;
}
}}catch(Exception e){
System.out.println("--------------------------------");
}
dis.close();
dos.close();
scanner.close();
}
服务器端:
package com.mec.complex.listener;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
public class Server implements Runnable{
private ServerSocket server;
private DataInputStream dis;
private DataOutputStream dos;
private boolean goon;
private Socket client;
private String clientip;
public Server(){
}
public void start(){
try {
System.out.println("开始建立服务器");
server = new ServerSocket(22222);
System.out.println("-------------------------------");
System.out.println("开始侦听客户端消息");
System.out.println("-------------------------------");
goon = true;
new Thread(this).start();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void run() {
while(goon){
try {
client = server.accept();
// Coversation coversation = new Coversation();
// coversation.start();
clientip = client.getInetAddress().getHostAddress();
System.out.println("-------------------------------");
System.out.println("侦听到客户端"+clientip+"连接请求");
System.out.println("-------------------------------");
coversation( client, clientip);
} catch (IOException e) {
System.out.println(clientip+"客户端已下线");
stoplistening();
}
}
}
public void coversation(Socket client,String clientip) throws IOException{
String mess = null;
dis = new DataInputStream(client.getInputStream());
dos = new DataOutputStream(client.getOutputStream());
mess = dis.readUTF();
System.out.println("-------------------------------");
System.out.println("收到客户端"+clientip+"消息:"+mess);
System.out.println("-------------------------------");
String s = new BufferedReader(new InputStreamReader(System.in)).readLine();
dos.writeUTF(s);
if (mess.equals("bye")){
goon = false;
System.out.println("服务器端下线-------");
stoplistening();
}else{
dos.writeUTF(" 服务器收到消息为:"+mess);
}
}
public void stoplistening() {
goon = false;
if(dis !=null){
try {
dis.close();
} catch (IOException e) {
}finally{
dis = null;
}
}
if(dos!=null){
try {
dos.close();
} catch (IOException e) {
}finally{
dos = null;
}
}
}
}
他们之间的通讯层:
package com.mec.complex.listener;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
public class Coversation implements Runnable {
private boolean goon=true;
private Socket client;
private DataInputStream dis;
private DataOutputStream dos;
public void start() throws IOException{
dis = new DataInputStream(client.getInputStream());
dos = new DataOutputStream(client.getOutputStream());
new Thread(this).start();
}
@Override
public void run() {
String mess = null;
String clientip = client.getInetAddress().getHostAddress();
System.out.println("-------------------------------");
System.out.println("侦听到客户端"+clientip+"连接请求");
System.out.println("-------------------------------");
while(goon){
try {
mess = dis.readUTF();
System.out.println("-------------------------------");
System.out.println("收到客户端"+clientip+"消息:"+mess);
System.out.println("-------------------------------");
if (mess.equals("bye")){
goon = false;
System.out.println("客户端下线了");
stoplistening();
}else{
dos.writeUTF(" 服务器收到消息为:"+mess);
}
} catch (IOException e) {
System.out.println("未成功收到"+clientip+"的消息");
stoplistening();
}
}
}
public void stoplistening() {
goon = false;
if(dis !=null){
try {
dis.close();
} catch (IOException e) {
}finally{
dis = null;
}
}
if(dos!=null){
try {
dos.close();
} catch (IOException e) {
}finally{
dos = null;
}
}
}
}
测试类
客户端:
package com.mec.complex.listener;
import java.io.IOException;
public class TeatClient {
public static void main(String[] args) throws IOException {
Client client = new Client();
client.start();
}
}
服务器端
package com.mec.complex.listener;
public class TeatSever {
public static void main(String[] args) {
Server server =new Server();
server.start();
server.run();
}
}