Maven-使用Maven建立聊天室工程
Socket 套接字使用TCP提供了两台计算机之间的通信,客户端创建一个套接字并连接服务器端的套接字.
Socket表示一个套接字,java.net.ServerSocket 类为服务器程序提供了一种来监听客户端,并与他们建立连接的机制。
2.服务器端的SeverSocket对象调用accept方法,等待客户端连接服务器的端口。
3.在客户端实例化一个Socket对象,并指定服务器名称和端口号来建立连接。 eg : Socket client = new Socket("127.0.0.1",6666);
4.若3中连接成功,则在服务器端中,accept将返回一个Socket对象,该socket连接到客户端的socket。 eg : Socket client = server.accept();
至此,服务端与客户端的连接已经建立成功。
服务端和客户端socket交互时用到的方法:
1 public void connect(SocketAddress host, int timeout) throws IOException 将此套接字连接到服务器,并指定一个超时值。
2 public InetAddress getInetAddress() 返回套接字连接的地址。
3 public int getPort() 返回此套接字连接到的远程端口。
4 public int getLocalPort() 返回此套接字绑定到的本地端口。
5 public SocketAddress getRemoteSocketAddress() 返回此套接字连接的端点的地址,如果未连接则返回null。
6 public InputStream getInputStream() throws IOException 返回此套接字的输入流。
7 public OutputStream getOutputStream() throws IOException 返回此套接字的输出流。
8 public void close() throws IOException 关闭此套接字。
格式:
userName:username //注册
G:msg //群发
Socket表示一个套接字,java.net.ServerSocket 类为服务器程序提供了一种来监听客户端,并与他们建立连接的机制。
步骤:
1.服务器实例化一个SeverSocket对象,表示服务器上的端口通信。2.服务器端的SeverSocket对象调用accept方法,等待客户端连接服务器的端口。
3.在客户端实例化一个Socket对象,并指定服务器名称和端口号来建立连接。 eg : Socket client = new Socket("127.0.0.1",6666);
4.若3中连接成功,则在服务器端中,accept将返回一个Socket对象,该socket连接到客户端的socket。 eg : Socket client = server.accept();
至此,服务端与客户端的连接已经建立成功。
服务端和客户端socket交互时用到的方法:
1 public void connect(SocketAddress host, int timeout) throws IOException 将此套接字连接到服务器,并指定一个超时值。
2 public InetAddress getInetAddress() 返回套接字连接的地址。
3 public int getPort() 返回此套接字连接到的远程端口。
4 public int getLocalPort() 返回此套接字绑定到的本地端口。
5 public SocketAddress getRemoteSocketAddress() 返回此套接字连接的端点的地址,如果未连接则返回null。
6 public InputStream getInputStream() throws IOException 返回此套接字的输入流。
7 public OutputStream getOutputStream() throws IOException 返回此套接字的输出流。
8 public void close() throws IOException 关闭此套接字。
客户端与服务端的源代码如下:(服务端和客户端建立两个工程)
--服务器端:
package com.chant;
import java.io.IOException;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
//userName:username
//G:msg
//P:pName-msg
public class Server {
//存放客户的Socket
private static Map<String,Socket> map = new HashMap<String,Socket>();
//静态内部类线程
static class ExecuteClientSever implements Runnable{
private Socket client;
public ExecuteClientSever(Socket client) {
this.client = client;
}
public void run() {
try {
PrintStream ps = new PrintStream(client.getOutputStream());
Scanner scanner = new Scanner(client.getInputStream());
while(true){
String str = null;
if(scanner.hasNext()){
str = scanner.next();
Pattern pattern = Pattern.compile("\r");
Matcher matcher = pattern.matcher(str);
str = matcher.replaceAll("");
}
if(str.startsWith("userName")){
String userName = str.split("\\:")[1];
userRegist(userName);
}
else if (str.startsWith("G")){
String msg = str.split("\\:")[1];
groupChat(msg);
}
else if(str.startsWith("P")){
String tmp = str.split("\\:")[1];
String pName = tmp.split("-")[0];
String msg = tmp.split("-")[1];
privateChat(pName,msg);
}
else if(str.startsWith("bey")){
scanner.close();
ps.close();
break;
}
else{
System.out.println("输入格式不对");
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
private void userRegist(String userName){
map.put(userName,this.client);
System.out.println("用户"+userName+"上线了。 当前人数:"+map.size()+"人");
}
private void groupChat(String msg) throws IOException {
Set<Map.Entry<String,Socket>> set = map.entrySet();
Iterator<Map.Entry<String,Socket>> it = set.iterator();
while(it.hasNext()){
Map.Entry<String,Socket> entry = it.next();
Socket client = entry.getValue();
PrintStream ps = new PrintStream(client.getOutputStream(),true,"UTF8");
String str = ("用户说: "+msg).trim();
ps.println(str);
}
}
private void privateChat(String pName, String msg) throws IOException{
Socket pClient = map.get(pName);
if(pClient!=null){
PrintStream ps = new PrintStream(pClient.getOutputStream(),true,"UTF8");
ps.println("Private: "+msg);
}else{
PrintStream ps = new PrintStream(this.client.getOutputStream());
ps.println("你要找的人不存在。");
}
}
}
public static void main(String[] args) throws IOException {
ExecutorService executorService = Executors.newFixedThreadPool(20);
ServerSocket server = new ServerSocket(6666);
for(int i=0;i<20;i++){
System.out.println("等待客户端连接。");
Socket client = server.accept();
executorService.execute(new ExecuteClientSever(client));
}
executorService.shutdown();
server.close();
}
}
--客户端:package com.chang.client;
import java.io.IOException;
import java.io.PrintStream;
import java.net.Socket;
import java.util.Scanner;
class ReadServer implements Runnable{
private Socket client;
public ReadServer(Socket client) {
this.client = client;
}
public void run() {
try {
Scanner scan = new Scanner(client.getInputStream());
while(true){
String str = scan.nextLine();
//if(scan.hasNext()){
System.out.println(str);
//}
if(client.isClosed()){
break;
}
}
scan.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
class Write implements Runnable{
private Socket client;
public Write(Socket client) {
this.client = client;
}
public void run() {
try {
PrintStream ps = new PrintStream(client.getOutputStream());
Scanner scanner = new Scanner(System.in);
while(true){
Thread.sleep(100);
System.out.println("请输入:");
String str = scanner.next().trim();
ps.println(str);
if(str.equals("bey")){
System.out.println("客户端关闭。");
ps.close();
scanner.close();
client.close();
System.exit(0);
}
}
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public class Client {
public static void main(String[] args) throws IOException {
Socket client = new Socket("127.0.0.1",6666);
new Thread(new ReadServer(client)).start();
new Thread(new Write(client)).start();
}
}
先运行服务端,在运行客户端,通过指定的格式输入,程序已经可以正常运行格式:
userName:username //注册
G:msg //群发
P:pName-msg //私聊
接下来我们使用Maven的方式建立工程
1.在IDEA下新建一个Maven工程。
到此,IDEA已经为我们建立好了一个基本的框架,接下来我们只要将需要的代码,包名手动添加就好了
2.添加源程序
服务器端的maven工程就已经创建好了。
3.编译, 运行,打包,发布等。。
执行没什么说的,和以前一样在Main方法处执行即可
在IDEA的右侧点开Maven Projects,其中包含了我们所需用的很多指令和插件
Sever端的Maven工程创建和使用如上,Client端的操作与之一样。