想用JAVA做一个服务器,请问怎么利用TCP和线程,实现多个客户端同时在线,能与服务器进行交互?
服务器监听端口 做个无限循环 接到一个连接就创建一个通道线程,并将通道线程存储到一个list集合中
import javaioBufferedReader;import javaioIOException;
import javaioInputStreamReader;
import javaioPrintWriter;
import javanetServerSocket;
import javanetSocket;
import javatextSimpleDateFormat;
import javautilArrayList;
import javautilDate;
import javautilList;
/
4用socket通讯写出多个客户端和一个服务器端的通讯,
要求客户发送数据后能够回显相同的数据(回显功能)(实用TCP方式)。
/
public class Test4Server {
// 主入口
public static void main(String[] args) throws IOException {
scoketServer();
}
// 开启的tcp8888监听端口
public static void scoketServer() throws IOException {
ServerSocket server = new ServerSocket(8888);
while (true) {
// 未连通前线程阻塞,连通后开启一个socket通道线程后继续监听8888端口
Socket socket = serveraccept();
Systemoutprintln(socketgetInetAddress()getHostAddress()
+ "连接进入");
new SocketThread(socket)start();
}
}
}
// 一个服务器端口中监听多个客服端通道线程
class SocketThread extends Thread {
// 所有通道写入流的集合
private static List<PrintWriter> list =new ArrayList<PrintWriter>();
private BufferedReader bufferedReader;
private PrintWriter printWriter;
public SocketThread(Socket socket) throws IOException {
thisbufferedReader = new BufferedReader(new InputStreamReader(socket
getInputStream()));
thisprintWriter = new PrintWriter(socketgetOutputStream());
listadd(printWriter);
}
@Override
public void run() {
String string = null;
while (true) {
try {
// 服务器在通道中读到的信息回显给客服端
string = bufferedReaderreadLine();
Systemoutprintln("客服端信息:" + string);
for(PrintWriter printWriter:list ){
printWriterwrite("服务器回显:" + string + "\r\n");
printWriterflush();
}
} catch (IOException e) {
}
}
}
}
客服端代码 可以用在局域网中用多台来连接测试
import javaioBufferedReader;import javaioIOException;
import javaioInputStreamReader;
import javaioPrintWriter;
import javanetSocket;
import javautilScanner;
public class Test4Client {
public static Object obj = new Object();
// 客服端的主入口
public static void main(String[] args) throws IOException {
socketClient();
}
// 与服务器连通地址本机(127001),局域网中其他机器是(服务器在局域网中的ip地址) 端口都是8888
public static void socketClient() throws IOException {
Socket socket = new Socket("127001", 8888);
if (socketisConnected()) {
// 如果连接成功了就开启写和读的进程
new writer(socket)start();
new read(socket)start();
} else {
Systemoutprintln("服务器未开启");
}
}
}
// 写入到通道的线程
class writer extends Thread {
@SuppressWarnings("unused")
private Socket socket;
private PrintWriter printWriter;
private Scanner scanner = new Scanner(Systemin);
private String str = null;
public writer(Socket socket) throws IOException {
thissocket = socket;
thisprintWriter = new PrintWriter(socketgetOutputStream());
}
@Override
public void run() {
scanneruseDelimiter("\r\n");
while (true) {
Systemoutprint("请输入信息:");
// 产生扫描器的线程阻塞
str = scannernext();
Systemoutprintln("我说:"+str);
printWriterwrite(str + "\r\n");
printWriterflush();
try {
Threadsleep(200);
} catch (InterruptedException e) {
eprintStackTrace();
}
}
}
}
// 从通道中读取的线程
class read extends Thread {
private Socket socket;
private BufferedReader bufferedReader;
private String str = null;
public read(Socket socket) throws IOException {
thissocket = socket;
thisbufferedReader = new BufferedReader(new InputStreamReader(socket
getInputStream()));
}
@Override
public void run() {
while (true) {
try {
str = bufferedReaderreadLine();
Systemoutprintln(str);
} catch (IOException e) {
}
try {
Threadsleep(200);
} catch (InterruptedException e) {
eprintStackTrace();
}
}
}
}
下面是一个简单的通讯实例,进行Server和Client之间的文件传输。。如果是简单的文本传输的话简化掉文本操作的内容即可。。
1服务器端
package sterning;
import javaioBufferedInputStream;
import javaioDataInputStream;
import javaioDataOutputStream;
import javaioFile;
import javaioFileInputStream;
import javanetServerSocket;
import javanetSocket;
public class ServerTest {
int port = 8821;
void start() {
Socket s = null;
try {
ServerSocket ss = new ServerSocket(port);
while (true) {
// 选择进行传输的文件
String filePath = "D:\\librar";
File fi = new File(filePath);
Systemoutprintln("文件长度:" + (int) filength());
// public Socket accept() throws
// IOException侦听并接受到此套接字的连接。此方法在进行连接之前一直阻塞。
s = ssaccept();
Systemoutprintln("建立socket链接");
DataInputStream dis = new DataInputStream(new BufferedInputStream(sgetInputStream()));
disreadByte();
DataInputStream fis = new DataInputStream(new BufferedInputStream(new FileInputStream(filePath)));
DataOutputStream ps = new DataOutputStream(sgetOutputStream());
//将文件名及长度传给客户端。这里要真正适用所有平台,例如中文名的处理,还需要加工,具体可以参见Think In Java 4th里有现成的代码。
pswriteUTF(figetName());
psflush();
pswriteLong((long) filength());
psflush();
int bufferSize = 8192;
byte[] buf = new byte[bufferSize];
while (true) {
int read = 0;
if (fis != null) {
read = fisread(buf);
}
if (read == -1) {
break;
}
pswrite(buf, 0, read);
}
psflush();
// 注意关闭socket链接哦,不然客户端会等待server的数据过来,
// 直到socket超时,导致数据不完整。
fisclose();
sclose();
Systemoutprintln("文件传输完成");
}
} catch (Exception e) {
eprintStackTrace();
}
}
public static void main(String arg[]) {
new ServerTest()start();
}
}
2socket的Util辅助类
package sterning;
import javanet;
import javaio;
public class ClientSocket {
private String ip;
private int port;
private Socket socket = null;
DataOutputStream out = null;
DataInputStream getMessageStream = null;
public ClientSocket(String ip, int port) {
thisip = ip;
thisport = port;
}
/ //
创建socket连接
@throws Exception
exception
/
public void CreateConnection() throws Exception {
try {
socket = new Socket(ip, port);
} catch (Exception e) {
eprintStackTrace();
if (socket != null)
socketclose();
throw e;
} finally {
}
}
public void sendMessage(String sendMessage) throws Exception {
try {
out = new DataOutputStream(socketgetOutputStream());
if (sendMessageequals("Windows")) {
outwriteByte(0x1);
outflush();
return;
}
if (sendMessageequals("Unix")) {
outwriteByte(0x2);
outflush();
return;
}
if (sendMessageequals("Linux")) {
outwriteByte(0x3);
outflush();
} else {
outwriteUTF(sendMessage);
outflush();
}
} catch (Exception e) {
eprintStackTrace();
if (out != null)
outclose();
throw e;
} finally {
}
}
public DataInputStream getMessageStream() throws Exception {
try {
getMessageStream = new DataInputStream(new BufferedInputStream(socketgetInputStream()));
return getMessageStream;
} catch (Exception e) {
eprintStackTrace();
if (getMessageStream != null)
getMessageStreamclose();
throw e;
} finally {
}
}
public void shutDownConnection() {
try {
if (out != null)
outclose();
if (getMessageStream != null)
getMessageStreamclose();
if (socket != null)
socketclose();
} catch (Exception e) {
}
}
}
3客户端
package sterning;
import javaioBufferedOutputStream;
import javaioDataInputStream;
import javaioDataOutputStream;
import javaioFileOutputStream;
public class ClientTest {
private ClientSocket cs = null;
private String ip = "localhost";// 设置成服务器IP
private int port = 8821;
private String sendMessage = "Windwos";
public ClientTest() {
try {
if (createConnection()) {
sendMessage();
getMessage();
}
} catch (Exception ex) {
exprintStackTrace();
}
}
private boolean createConnection() {
cs = new ClientSocket(ip, port);
try {
csCreateConnection();
Systemoutprint("连接服务器成功!" + "\n");
return true;
} catch (Exception e) {
Systemoutprint("连接服务器失败!" + "\n");
return false;
}
}
private void sendMessage() {
if (cs == null)
return;
try {
cssendMessage(sendMessage);
} catch (Exception e) {
Systemoutprint("发送消息失败!" + "\n");
}
}
private void getMessage() {
if (cs == null)
return;
DataInputStream inputStream = null;
try {
inputStream = csgetMessageStream();
} catch (Exception e) {
Systemoutprint("接收消息缓存错误\n");
return;
}
try {
//本地保存路径,文件名会自动从服务器端继承而来。
String savePath = "E:\\";
int bufferSize = 8192;
byte[] buf = new byte[bufferSize];
int passedlen = 0;
long len=0;
savePath += inputStreamreadUTF();
DataOutputStream fileOut = new DataOutputStream(new BufferedOutputStream(newBufferedOutputStream(new FileOutputStream(savePath))));
len = inputStreamreadLong();
Systemoutprintln("文件的长度为:" + len + "\n");
Systemoutprintln("开始接收文件!" + "\n");
while (true) {
int read = 0;
if (inputStream != null) {
read = inputStreamread(buf);
}
passedlen += read;
if (read == -1) {
break;
}
//下面进度条本为图形界面的prograssBar做的,这里如果是打文件,可能会重复打印出一些相同的百分比
Systemoutprintln("文件接收了" + (passedlen 100/ len) + "%\n");
fileOutwrite(buf, 0, read);
}
Systemoutprintln("接收完成,文件存为" + savePath + "\n");
fileOutclose();
} catch (Exception e) {
Systemoutprintln("接收消息错误" + "\n");
return;
}
}
public static void main(String arg[]) {
new ClientTest();
}
}
如何用java 5分钟实现一个最简单的mysql代理服务器
首先,准备开发工具套件,我们并不会引入过多工具包,仅仅需要:
java8
vertx 3
如果你是用maven做为项目管理工具,请将vertx 3引入:
1
2
3
4
5
<dependency>
<groupId>iovertx</groupId>
<artifactId>vertx-core</artifactId>
<version>332</version>
</dependency>
代码实现:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package
commaxleapmysqlproxy;
import
iovertxcoreAbstractVerticle;
import
iovertxcoreVertx;
import
iovertxcoreloggingLogger;
import
iovertxcoreloggingLoggerFactory;
import
iovertxcorenetNetClient;
import
iovertxcorenetNetServer;
import
iovertxcorenetNetSocket;
/
@author sneaky
@since 100
/
public
class
MysqlProxyServer
{
private
static
final
Logger
logger
=
LoggerFactorygetLogger(MysqlProxyServerclass);
public
static
void
main(String[]
args)
{
Vertxvertx()deployVerticle(new
MysqlProxyServerVerticle());
}
public
static
class
MysqlProxyServerVerticle
extends
AbstractVerticle
{
private
final
int
port
=
3306;
private
final
String
mysqlHost
=
"101006";
@Override
public
void
start()
throws
Exception
{
NetServer
netServer
=
vertxcreateNetServer();//创建代理服务器
NetClient
netClient
=
vertxcreateNetClient();//创建连接mysql客户端
netServerconnectHandler(socket
->
netClientconnect(port,
mysqlHost,
result
->
{
//响应来自客户端的连接请求,成功之后,在建立一个与目标mysql服务器的连接
if
(resultsucceeded())
{
//与目标mysql服务器成功连接连接之后,创造一个MysqlProxyConnection对象,并执行代理方法
new
MysqlProxyConnection(socket,
resultresult())proxy();
0条评论