易语言服务器如何监听客户端口
3个回答
展开全部
可以考虑用JAVA实现监听客户端口,参考例子如下:
package test;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Method;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
/**
* 该程序用的DatagramSocket,实现UDP的通讯
* @author D'Addario
*
*/
public class Test{
private int headLen = 12;//消息起点和终点的记录 例如classAclassB就是发往classA的消息,发送者是classB
private Random random;
private String serverIp;
private int serverPort;
private HashMap<String,Classes> map ;
{
serverIp = "127.0.0.1";
serverPort = 9999;
random = new Random(System.currentTimeMillis());
map = new HashMap<String,Classes>();
Classes A = Test.this.new Classes();
A.ip = "127.0.0.1";
A.name = "我是A";
A.port = 10000;
map.put("classA", A);
Classes B = Test.this.new Classes();
B.ip = "127.0.0.1";
B.name = "我是B";
B.port = 10001;
map.put("classB", B);
}
public static void main(String[] a) throws Exception{
Test test = new Test();
new Thread(test.new Server()).start();
Client client1 = test.new Client();
client1.mine = "classA";
client1.remote = "classB";
new Thread(client1).start();
Client client2 = test.new Client();
client2.mine = "classB";
client2.remote = "classA";
new Thread(client2).start();
}
/**
* 成员 用于记录 每个成员的个性信息
* @author D'Addario
*
*/
private class Classes {
private String name ; //成员姓名
private String ip ; //成员ip
private int port;//成员的client server 监听端口
}
/**
* server 用于转发两个client之间消息的类
* @author D'Addario
*
*/
private class Server implements Runnable{
public void run() {
// TODO Auto-generated method stub
boolean start = false;
DatagramSocket socket = null;
try {
socket = new DatagramSocket(serverPort);
start = true;
} catch (SocketException e1) {
e1.printStackTrace();
}
while(start)
{
try {
byte[] data = new byte[1024];
DatagramPacket packet = new DatagramPacket(data,data.length);
socket.receive(packet);
int len = packet.getLength();
//System.out.println("recevie data:"+new String(data,0,len));
if(len<headLen)
throw new IOException("无效数据");
String id = new String(data,0,headLen/2);//获取发往对象的ID
String id2 = new String(data,headLen/2,headLen/2);
System.out.println("receive from "+id2 +", send to "+id+",msg["+new String(data,headLen,len-headLen)+"]");
Classes one = map.get(id.trim());
DatagramPacket retPack = new DatagramPacket(data,0,len,InetAddress.getByName(one.ip),one.port);
socket.send(retPack);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
/**
* Client 客户端类,用于客户端消息的发送
* @author D'Addario
*
*/
private class Client implements Runnable{
private String mine;
private String remote;
public void run() {
// TODO Auto-generated method stub
String msg = remote+mine;
DatagramSocket socket = null;
try {
socket = new DatagramSocket(map.get(mine).port);
ClientServer srv=new ClientServer();
srv.setSocket(socket);
new Thread(srv).start();
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
byte[] data ;
while(true)
{
String sendStr = msg + "给我"+random.nextInt(5000)+"分吧";
data = sendStr.getBytes();
try {
DatagramPacket retPack = new DatagramPacket(data,0,data.length,InetAddress.getByName(serverIp),serverPort);
socket.send(retPack);
Thread.sleep(random.nextInt(5)*1000);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
/**
* 客户端server 用于监听服务器的消息
* @author D'Addario
*
*/
private class ClientServer implements Runnable{
DatagramSocket socket ;
public DatagramSocket getSocket() {
return socket;
}
public void setSocket(DatagramSocket socket) {
this.socket = socket;
}
public void run() {
byte[] data = new byte[1024];
DatagramPacket packet = new DatagramPacket(data,data.length);
while(true){
try {
socket.receive(packet);
int len = packet.getLength();
if(len<headLen)
throw new IOException("无效数据");
System.out.println("I get the message :"+Thread.currentThread().getId()+"-->["+new String(data,0,len)+"]");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} }
}
}
}
Storm代理
2023-07-25 广告
2023-07-25 广告
StormProxies是一家可靠的代理服务提供商,提供原生IP(住宅原生IP)和高匿名代理服务。以下是关于StormProxies的原生IP服务的一些信息:1. 住宅原生IP:StormProxies提供的住宅原生IP是指从真实的家庭或企...
点击进入详情页
本回答由Storm代理提供
展开全部
你好,很高兴为你解答。
小弟听不懂你的意思,客户组件好像不用设置端口吧?只是服务器组件才可以监视端口
让客户连接。
小弟听不懂你的意思,客户组件好像不用设置端口吧?只是服务器组件才可以监视端口
让客户连接。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
监听,没有理解你说的是什么。
不过你可以在客户端那边写上代码
客户端按键(A)
然后就让客户端给服务器发送(xxx按键(A))
不过你可以在客户端那边写上代码
客户端按键(A)
然后就让客户端给服务器发送(xxx按键(A))
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询