我尝试从套接字发送不同的消息,但结果并不像我想的那样(像大家一样计算),我不确定原因。
当我使用变量来测试返回数据的数量时,第一次的结果是正确的,但它可能在第一次之后跳过或不执行任何操作,就像int test=0; test++;
和{{1}的结果一样应该是test
(就像第一次发送一样),但它变为1 2 3 4 5 6 7 8 9
(当我重新发送套接字消息时)。
我的代码是:
在main.java中:
1 2 2 4 5 6 7 7 9
和SocketTest.java:
static DatagramSocket socket2;
static boolean isRun=true;
static String sends="message#";
static int test=0;
public static void main(String[] args) {
// TODO Auto-generated method stub
try{
socket2 = new DatagramSocket(10000);
socket2.setBroadcast(true);
}catch(Exception ex){
System.out.println("Server side problem occured: " + ex.getMessage());
}
SendType1();
}
public static void SendType1(){
test=0;
sends = "message#";
listen();
SocketTest net= new SocketTest();
net.sendMessage(sends);
}
public static void SendType2(){
test=0;
sends = "message2#";
listen2();
SocketTest net= new SocketTest();
net.sendMessage(sends);
}
public static void listen() {
new Thread() {
public void run() {
try{
byte[] recvBuf = new byte[1024];
while (isRun) {
DatagramPacket packet2 = new DatagramPacket(recvBuf, recvBuf.length);
try{
socket2.receive(packet2);
String message = new String(trimByte(packet2.getLength(),packet2.getData()));
if(message.startsWith("RE_" + sends)){
System.out.println(test+":"+message); // the result doesn't as I think
test++;
}
}catch(Exception ex){
System.out.println("Cannot receive package: " + ex.getMessage());
System.exit(0);
}
}
}catch(Exception ex){
System.out.println("Server side problem occured: " + ex.getMessage());
}
}
}.start();
}
public static void listen2() {
new Thread() {
public void run() {
try{
byte[] recvBuf = new byte[1024];
while (true) {
DatagramPacket packet2 = new DatagramPacket(recvBuf, recvBuf.length);
try{
socket2.receive(packet2);
String message = new String(trimByte(packet2.getLength(),packet2.getData()));
if(message.startsWith("RE_" + sends)){
// do something
}
}catch(Exception ex){
System.out.println("Cannot receive package: " + ex.getMessage());
}
}
}catch(Exception ex){
System.out.println("Server side problem occured: " + ex.getMessage());
}
}
}.start();
}
private static byte[] trimByte(int length, byte[] tmp){
byte[] ok=new byte[length];
for(int i=0;i<length;i++){
ok[i]=tmp[i];
}
return ok;
}
}
我使用public class SocketTest {
DatagramSocket socket;
static List<String[]> listNet = new ArrayList<String[]>();
public SocketTest(){
try {
socket = new DatagramSocket();
socket.setBroadcast(true);
socket.connect(InetAddress.getByName("255.255.255.255"), 10000);
} catch (Exception e) {
System.out.println("Connection failed. " + e.getMessage());
}
}
public void sendMessage(String message){
byte[] buf= message.getBytes();
DatagramPacket packet;
try{
packet= new DatagramPacket(buf, buf.length);
socket.send(packet);
//System.out.println("Socket send datas.");
}catch(Exception e){
System.out.println("Sending failed. " + e.getMessage());
}
}
作为用户界面来发送不同的消息,但我认为这部分很容易并且不会影响问题,就像用户更改标签到&#39; Type1&#39;发送消息为&#39;消息#&#39;并将标签更改为&#39; Type2&#39;发送消息为&#39; message2#&#39;。
我收到的邮件是正确的,但我需要变量tabbedPane
才能显示正确的组件(例如按钮和标签),如果test
没有计数,我就无法设置布局得当。
我是否使用错误的方式发送不同的消息?
我没有找到任何问题作为test
结果(或者我自己误导了主要问题)。
任何帮助将不胜感激。
答案 0 :(得分:0)
我的错,我应该只使用一个线程来接收消息,而不是使用多个线程进行每次不同的发送,我应该通过if
,else
确定返回的消息。计数器(变量test
)也可以用于不同的发送!
所以我只需将main.java代码更改为:
static DatagramSocket socket2;
static JFrame demo = new JFrame();
static JPanel myPanel=new JPanel();
static JPanel myPanel2=new JPanel();
static JTabbedPane tabbedPane = new JTabbedPane();
static JScrollPane pane = new JScrollPane(tabbedPane);
static boolean isRun=true;
static int test=0;
static int test2=0;
public static void main(String[] args) {
// TODO Auto-generated method stub
demo.setSize(600, 300);
demo.setTitle("Test");
demo.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
try{
socket2 = new DatagramSocket(10000);
socket2.setBroadcast(true);
}catch(Exception ex){
System.out.println("Server side problem occured: " + ex.getMessage());
}
SendMsg("message#");
// Part of tabbedPane to change sending message
tabbedPane.addTab("Type1", myPanel);
myPanel.setLayout(new GridBagLayout());
tabbedPane.addTab("Type2", myPanel2);
myPanel2.setLayout(new GridBagLayout());
tabbedPane.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
if(tabbedPane.getSelectedIndex() == 0){
SendMsg("message#");
}else if(tabbedPane.getSelectedIndex() == 1){
SendMsg("message2#");
}
}
});
demo.getContentPane().add(pane);
demo.setLocationRelativeTo(null);
demo.setVisible(true);
}
public static void SendMsg(String data){
test2=0;
myPanel.removeAll();
myPanel.revalidate();
demo.repaint();
listen();
SocketTest net= new SocketTest();
net.sendMessage(data);
}
public static void listen() {
new Thread() {
public void run() {
try{
byte[] recvBuf = new byte[1024];
while (isRun) {
DatagramPacket packet2 = new DatagramPacket(recvBuf, recvBuf.length);
try{
socket2.receive(packet2);
String message = new String(trimByte(packet2.getLength(),packet2.getData()));
if(message.startsWith("RE_message#")){
test2++;
System.out.println(test2); // the counter is okay now
}else if(message.startsWith("RE_message2#")){
test++;
System.out.println(test+"); // the counter is okay now
}
}catch(Exception ex){
System.out.println("Cannot receive package: " + ex.getMessage());
}
}
}catch(Exception ex){
System.out.println("Server side problem occured: " + ex.getMessage());
}
}
}.start();
}
private static byte[] trimByte(int length, byte[] tmp){
byte[] ok=new byte[length];
for(int i=0;i<length;i++){
ok[i]=tmp[i];
}
return ok;
}
所以我想我使用两个线程然后每个线程相互影响导致问题。