I have a very strange problem - A can listen on B, but B cannot listen on A

Hello!
I have a very disturbing problem.
I have done a Paint-like program that add Point-objects on the JFrame. This porgram is networked and it contains a thread class that is sending the Point and a thread class that recieves Point objects. That makes it possible to open two instances of the program and when you paint on one of the windows, the program will send the point coordinats through a DatagramPacket to the other window. So, to make this clear, this program has ONE Sending class called Client and ONE Receiving class called Server. So when you want to make a connection, then you have to open two instances of the program.
So what is the problem?
Suppose that I have opened two instances of the program. Instance A is sending packets to port 2001 and is listening for packets on port 2000. Instance B is sending packets to port 2000 and is listening for packets on port 2001.
When I draw a point on Instance A's window, the Instance B is receiving the point and draws the point on its window.
When I draw a point on Instance B's window, the Instance A is NOT receiving the point. Instance B is listening on itself. That is the problem. Why is instance B listening for packets from itself and why doesn´t A get B's packets when B gets A's packets?
Here is the send och receive classes (called Client and Server):
class Client extends Thread
            Point p;
            Client(Point p)          
                 this.p = p;
            public void run()
                 System.out.println("Sending");
                 try
                      InetAddress toHost = InetAddress.getLocalHost();                     
                      int port = 2000;                    
                      DatagramSocket sock = new DatagramSocket();                                                   
                      String message = Integer.toString(p.x) + " " + Integer.toString(p.y);                         
                      byte[] data = message.getBytes();                    
                      DatagramPacket p = new DatagramPacket(data, data.length, toHost, port);     
                      sock.send(p);                                                                                
                 catch(SocketException se){}
                 catch(IOException ioe){}
                 catch(ConcurrentModificationException cme){}
class Server extends Thread               
            String message;
            String[] xy;
            public void run()
                 System.out.println("Receiving");
                 try
                      byte[] data = new byte[8192];
                      DatagramPacket packet = new DatagramPacket(data, data.length);
                      DatagramSocket sock = new DatagramSocket(2001);
                      while(true)     
                           sock.receive(packet);                                                                                            
                           message = new String(packet.getData(), 0, packet.getLength());                                          
                           xy = message.split(" ");                                                                 
                           Point p = new Point(Integer.parseInt(xy[0]), Integer.parseInt(xy[1]));          
                           addPoint(p);                                                                                               
                 catch(SocketException se){}
                 catch(IOException ioe){}
                 catch(ConcurrentModificationException cme){}
  }I have tried to solve this for hours but I have not found any solution.
Please, help!

Here is an executable example.
1. To run the first instance: Compile the code and run the application and note that it is listening on port 2000 and sending through port 2001
2. To run the second instance: Change the ServerSocket to listen on port 2001 and the DatagramSocket to send through port 2000 and compile and run the application.
3. Tell me this: Why does the first instance listen from DatagramPackets from itself and not from the other running instance while the other running instance is listening for DatagramPackets sent from the first instance? The one who can give me the answer will be the man of the month.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.lang.Thread;
import java.net.*;
import java.net.DatagramPacket;
import java.io.*;
public class Paint extends JFrame
     private Board b = new Board();
     public static void main(String[] args)
          new Paint();
       public Paint()
          setDefaultCloseOperation(EXIT_ON_CLOSE);
          getContentPane().add(b, BorderLayout.CENTER);
          addKeyListener(new ChangeSizeListener());
          setSize(300, 300);
          setVisible(true);
          setFocusable(true);
       class ChangeSizeListener extends KeyAdapter
          Board b = new Board();
          public void keyPressed(KeyEvent e)
               if(KeyEvent.getKeyText(e.getKeyCode()).equalsIgnoreCase("s"))
                    try
                           int size = Integer.parseInt(JOptionPane.showInputDialog(null, "New size:", "Size", 3));
                           b.setSize(size);
                    }catch(NumberFormatException nfe){}
class Board extends JPanel
     Point p;
       private HashSet hs = new HashSet();
       int size_x;
       int size_y;
          public Board()
               size_x = 20;
               size_y = 20;
               setFocusable(true);
               setBackground(Color.white);
               addMouseListener(new L1());
               addMouseMotionListener(new L2());
               Server s = new Server();
               s.start();
          public void paintComponent(Graphics g)
               super.paintComponent(g);
               g.setColor(Color.black);
               Iterator i = hs.iterator();
               while(i.hasNext())
                 p = (Point)i.next();
                 g.fillOval(p.x, p.y, size_x, size_y);
         private void addPoint(Point p)
               hs.add(p);
               repaint();
         public void setSize(int size)
                this.size_x = size;
                this.size_y = size;
                repaint();
       class L1 extends MouseAdapter
            public void mousePressed(MouseEvent me)
                Client c = new Client(me.getPoint());
                 addPoint(me.getPoint());
                  c.start();
       class L2 extends MouseMotionAdapter
            public void mouseDragged(MouseEvent me)
                 Client c = new Client(me.getPoint());
                  addPoint(me.getPoint());
                  c.start();
       class Client extends Thread
            Point p;
            Client(Point p)
                 this.p = p;
            public void run()
                 System.out.println("Sending");
                 try
                      InetAddress toHost = InetAddress.getLocalHost();
                      int port = 2000;                 // After running a first instance of the application, change the port to listen on 2001
                      DatagramSocket sock = new DatagramSocket();
                      String message = Integer.toString(p.x) + " " + Integer.toString(p.y);
                      byte[] data = message.getBytes();
                      DatagramPacket p = new DatagramPacket(data, data.length, toHost, port);
                      sock.send(p);
                 catch(SocketException se){}
                 catch(IOException ioe){}
                 catch(ConcurrentModificationException cme){}
       class Server extends Thread
            String message;
            String[] xy;
            public void run()
                 System.out.println("Receiving");
                 try
                      byte[] data = new byte[8192];
                      DatagramPacket packet = new DatagramPacket(data, data.length);
                      DatagramSocket sock = new DatagramSocket(2001);              // After running a first instance of the application, change the DatagramSocket to listen on port 2000
                      while(true)
                         sock.receive(packet);
                         message = new String(packet.getData(), 0, packet.getLength());
                         xy = message.split(" ");
                         Point p = new Point(Integer.parseInt(xy[0]), Integer.parseInt(xy[1]));
                         addPoint(p);
                 catch(SocketException se){}
                 catch(IOException ioe){}
                 catch(ConcurrentModificationException cme){}
}

Similar Messages

  • Very strange problem w/ iTunes PC

    Well, I have a very strange problem - I added some music to my iTunes and when I was changing the informations and cover, my iTunes crashed. When I restarted the program, the album is missing and when I found it on my hard drive, I am not able to import it back to the iTunes. This happens with both Import from the menu and the classic Drag/Drop method. So what to do? Is there any way to get it back?
    EDIT: Tried to move the files to another folder. Didn't work though.

    I just found the problem lies with iTunes 7.0.
    I ran the sames songs (ones with problem) into iTunes 4.7.1, with MSN messenger (same version)'s song display selected.....the song title/artist displayed and it did not get kicked out!!!!
    If it's not my 80gig iPod.......I would not have gotten iTunes 7.0!!!!!!!

  • I have very strange problem with Iphone 3gs. Whenever it gets little hot or exposed to 30 degree temperature it losses signals (No service). Whenever it is cooled down the service is back. Please help!

    I have very strange problem with iphone 3gs. Whenever it gets little hot or exposed to 30 degree or above temperature it losses signals (No service). Whenever it is cooled down the service is back. Whenever I am using Skype or playing game for a while it losses carrier signals and wifi. When it is cooled down afer shutting down the carrier signals and wifi are back. It is for sure problem is with heating, but 30 degree tempeature in Oman is normal and some time it goes upto 50 degrees. I have noticed this has started after I started using substandard charger as I lost my original charger of the phone. Please help!! Thanx.

    The specifications define an operating range up to 35 degrees.

  • Nokia 3250:A very strange problem...

    hi..i'm facing a very strange problem in my handset 3250.. i've upgraded its os but still there is problem: http://discussions.europe.nokia.com/discussions/bo​ard/message?board.id=smartphones&;message.id=9102#M9102 any idea about such type of problem.....

    MR.,
    what is firmware?? How can i have its latest edition??
    Can you help me because i too have the same problem:
    Problem 1:
    1-I install any software (e.g:snake game) through Application manager (Pc Suit) in memory card.
    2-I launch that software (snake game) just after installation it works properly.
    3-Now i switch-off the hand set and than switch-on it.
    4-I try to launch that software (snake game) again but it doesn't response.
    Problem 2:
    1:There is a image in my Memory card.
    2: I set that image as a wallpaper.
    3-Now i switch-off the hand set and than switch-on it.
    4-Wallpaper reset to default one.
    Problem 3:
    1-I install a theme through Application manager (Pc Suit) in memory card.
    2: I apply that theme in my hand set.
    3-Now i switch-off the hand set and than switch-on it.
    4- Now theme reset to default one.

  • ALV Grid - Very Strange Problem

    I am facing a very strange problem in ALV Grid. Our’s is a ECC5 system on SAP_APPL SAPKH50011 and SAP_BASIS SAPKB64016.
    I have implemented the ALV grid in a screen using classes. This grid by default appears in display mode however it can be switched to change mode and back to display mode using a custom button on the ALV toolbar. When the user clicks the change button the grid appears in editable mode. During the back and forth switching of the ALV between Change/Display modes the ALV grid control is not destroyed however the toolbar, fieldcatalog and ALV contents are refreshed.
    The data which is entered in an editable cell is validated using the data_changed event which has been implemented locally. If the validation fails an error message is raised using the message statement which is being issued correctly. Now when I correct the data and then click on another cell in the ALV grid the program is being aborted. Any clue why this is happening??

    Please check this code
    In the PBO of the screen set the field catalog and layout , i think this will help not to refresh the field catalog every time swtich between display and change mode
        set titlebar sy-dynnr with p_netwk.
        call method g_grid->set_frontend_fieldcatalog
          exporting
            it_fieldcatalog = gt_fieldcat[].
        call method g_grid->set_frontend_layout
          exporting
            is_layout = gs_layout.
        call method g_grid->refresh_table_display.
    2. Regarding program abort, please paste the first page of system dump

  • [Mysql-Connector] A very strange problem !

    Hi everybody!
    Ready to hear a strange story ?
    I have a weird problem to use the MySQL-connector under Linux (Debian).
    I have no problem in windows XP, all is working fine !
    But under Linux when I try to connect to mysql throught mysql-connector wrote in a Servlet I have this message :
    Message: Invalid authorization specification message from server: "Access denied for user 'root'@'monkinetwork' (using password: YES)"
    SQLState: 28000
    ErrorCode: 1045 Here the software I'm using :
    Tomcat 5.0.28
    MySQL-Connector version is : mysql-connector-java-3.0.15-ga-bin.jar
    JDK Version : 1_5_0_01. Servlet-Examples and JSP works fine! So I don't think the problem come from JDK.
    MySQL version : MySQL-SERVER-4.1.9-2 : All is working under console mode !
    Here's My Servlet TESt1.java:
    import java.io.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    import java.sql.*;
    import java.sql.DriverManager;
    public class TEST1 extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException  {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
       String url = "jdbc:mysql://localhost:3306/HeroDB";
       String user = "root";
       String password = "password";
    try{
        Class.forName("com.mysql.jdbc.Driver");
        out.println("<br> DRIVERS JDBC : OK!");
        Connection connection = DriverManager.getConnection(url,user,password);
        out.println("<br> Database connection : OK!");
    catch (ClassNotFoundException e)
             out.println("Error with JDBC Drivers !");
    catch(SQLException ex) {
            out.println("<br> ERROR MESSAGE <br>");
       while (ex != null) {
                    out.println("<br>Message: " + ex.getMessage ());
                    out.println("<br>SQLState: "  + ex.getSQLState ());
                    out.println("<br>ErrorCode: "  + ex.getErrorCode ());
                    ex = ex.getNextException();
          out.println("");
    AND THE HTML PAGE in order to access to the Servlet :
    <HTML>
    <HEAD>
    <TITLE>DataBase Test</TITLE>
    </HEAD>
    <BODY BGCOLOR="#FDF5E6">
    <H2 ALIGN="CENTER">DataBase TEST</H2>
    <FORM ACTION="http://localhost:8080/TEST1">
    <CENTER>
    <INPUT TYPE="SUBMIT" VALUE = "CONNEXION TEST">
    </CENTER>
    </FORM>
    </BODY>
    </HTML>
    Theses codes works very well under windows, but under linux system here what I've got :
    DRIVERS JDBC : OK!
    ERROR MESSAGE
    Message: Invalid authorization specification message from server: "Access denied for user 'root'@'monkinetwork' (using password: YES)"
    SQLState: 28000
    ErrorCode: 1045 Well, the web.xml file is well configured.
    Anyway : I already tried with class: org.gjt.mm.mysql.driver, but I have the same message error !
    By the way, it's very strange that I can play with MySQL under the terminal but not throught tomcat.
    Any suggestions please , because it's giving me a very hard time ! ?
    Thank you !
    ++
    maki

    MySQL does authentication based on usernames and hosts. See http://dev.mysql.com/doc/mysql/en/connection-access.html

  • I have Adobe premiere 12 elements I have Buy it but this program is Corrupt. the are no sound on the time line. I have sound in my computer, this is no problem, I can listen every another things but NO Elements 12 OK I make ia video take down to the time

    i have Adobe premiere 12 elements I have Buy it but this program is Corrupt. the are no sound on the time line. I have sound in my computer, this is no problem, I can listen every another things but NO Elements 12 OK I make ia video take down to the time line. Video is OK BUT NO sound I have makit in 2 veeks from monday to next vek here whit this very bad program so i go to garbags I think I buy a another program is be better and have a Sound. This is very bad I am not god to English. I Have a pro camera and I will have a god support but this is very bad. I is bad to English. But this Program I buy i think this is very Corrupt. The mast find a sound in this program. I cvan not understan if You can sell this very bad program Videoredigering and this program have nothing sound. This is crazy.

    i have Adobe premiere 12 elements I have Buy it but this program is Corrupt. the are no sound on the time line. I have sound in my computer, this is no problem, I can listen every another things but NO Elements 12 OK I make ia video take down to the time line. Video is OK BUT NO sound I have makit in 2 veeks from monday to next vek here whit this very bad program so i go to garbags I think I buy a another program is be better and have a Sound. This is very bad I am not god to English. I Have a pro camera and I will have a god support but this is very bad. I is bad to English. But this Program I buy i think this is very Corrupt. The mast find a sound in this program. I cvan not understan if You can sell this very bad program Videoredigering and this program have nothing sound. This is crazy.

  • A very Strange Problem!!!help me!!

    i encounter a very strange problem, in EJB
    i write two EJB, one Stateless Session called A, and one Entity called B.
    i want to call B's findByPrimaryKey method in one A's Business, but failed!!!
    but when i remove the statement that performed the findByPrimaryKey method to A's setSessionContext method, It's Success!!!!!
    what the Problem, i am useing the Borland 's AppServer.
    who can help

    how u create the entity bean B from A?
    using proper lookup?
    can u try by write a small function inside bean A
    that contain proper lookup to Bean B...
    then try to call finbyPrimaryKey...
    now tell me is it working?
    or else can u give code or clear idea..
    if i can help u ,,,, sure i will
    do mail me
    [email protected]

  • A very strange problem in netbeans

    i debug a project in netbeans,a a very strange problem happens.
    a member variable was different between wtk20 and wtk22.and the variable was changed many places so it's hard to trace!
    but there hasn't the problem when i debug project in jbuilderx!

    My problem is, even I've written the SQL in the correct syntax, in my java code, it just look like this
    adc.SQL("insert into test(subject, content) values('" + sf.filter(subject.getText()) +"'"
                                  + "," + "'"
                                  + sf.filter(board.getText()) + "')" );
                                  subject.setText("");
                                  board.setText("");
    where subject is a textfield and board is a JTextArea and sf is a string filter which use regular expression, I basically want to insert the string in these component into the database, Many strings work, but client for updates can't be inserted.
    With my best,
    Zike Huang

  • Hi,i have a very big problem....i had an accident with my macbook , i poured some champagne and it does not start anymore....it will work again???

    hi,i have a very big problem....i had an accident with my macbook , i poured some champagne and it does not start anymore....it will work again???

    If you spilled champagne on it, chances are not good.  If you haven't alredy DO NOT turn it on or charge it.  I would immediately take it to the Genius Bar and see if they can clean it up before. 
    Had the same problem with a 15" MBP and a tea spill and I did the opposite.  Trashed the Logic & Keyboard Assembly.  Cost to repair was going to be more than what I paid for a computer so ended up having to replace it.

  • I tell her that I'm developing a flash content with as2 and I have a very simple problem, I hope you

    Hello, how are you?
    I tell her that I'm developing a flash content with as2 and I have a very simple problem, I hope you can help me
    I happened to create a movie clip and inside to create a scroll that is two isntancias,
    I mean:
    in the first frame create a button in the 6 to 5 buttons and an action to melleve the sixth frame and another to return. (Elemental truth)
    Now I have a problem. In creating a movie clip instance name to each button and in the average place the next fram as2,
    boton.onRollOver name = function () {
           gotoAndStop ("Scene 1", "eqtiqueta");
    because what I want is that from inside the movie clip, go to the scene proncipal to a frame that is three in the label name.
    however, does not work.
    appreciate your cooperation.
    Escuchar
    Leer fonéticamente
    Diccionario - Ver diccionario detallado

    Hello, I think you need to start a discussion on the Action Script forum. This is the Flash Player forum, the browser plugin.
    Thanks,
    eidnolb

  • I have a very similar problem (5506) in that I changed my appleID loginid and now none of my home shares work. All itunes have been re-authorized/authenticated with the new appleID string. Yet I still receive this error. I too am looking for suggestions.

    I have a very similar problem in that I changed my appleID loginid and now none of my home shares work (5506) . All itunes have been re-authorized/authenticated with the new appleID string. Yet I still receive this error. I too am looking for suggestions.

    If you no longer have the computer(s) you want to deauthorise,
    Log in to iTunes,  go to "view your account info" on the itunes store,  deauthorise all five, (Please Note: this can only be done Once every 12 months)  and then re-authorize your current Computer(s) one at a time.
    Authorise / Deauthorise About
    http://support.apple.com/kb/HT1420

  • I have a very big problem with my mac book pro. It continously looses the wifire connection. This happens every 5-10 minutes and is getting to be a very big irritation. I have tested my wifiresignal

    I have a very irritating problem with my MacBook Pro. It keeps loosing the wifi connection - every 5-1o minutes!!!
    I have testet the wifi signal and had my router checked - both are fine.
    Please tell me what I can do :-(
    John

    First you should check on another Wifi to see if it's your Lion machine.
    If it only occurs on your Wifi, then perhaps you have local interference and need to set the router on another channel far from others.
    You can install a free program called KisMAC that you set your Driver Preferences to Apple Airport Extreme Passive Mode, enter your Admin password and monitor your local Wifi signals, see if you cna pin point the cause of your interference or meddler.
    If your not running WPA2 with AES and a 12 plus random character/letter/number password for both Admin and Internet access use only then you should seriously consider doing so.
    http://www.tomshardware.com/reviews/wireless-security-hack,2981-10.html

  • Very strange problem facing

    Hello All,
    I am facing very strange problem from last a week.
    I have made flex application using Flex3 and AMFphp.
    I was running very fine but just from before 7 days, my all images will not displayed in Mozilla Firefox browser.
    but it is running fine in Internet Explorer browser.
    any idea? I have tried to know what happen but i failed.
    Please help me to identify what's going wrong.
    Thank you.

    Have you tried emptying your cache?
    Dany

  • Very strange problem / Perpetual command key

    I've been using OS X since its inception, and have never had this problem before.
    Starting yesterday, I noticed a very strange problem that was happening randomly. When I clicked on an app in the dock, it would just reveal the app to me in Finder rather than opening it. Other problems including multiple selections when single-clicking on items with my mouse, and not being able to type. After some fidgeting, I realized that my iMac is acting like I'm always holding down the command (apple) key. If I have a window open, for instance, pressing the H (just by itself) key will hide the window. Pressing Q will quit whatever is active.
    I shut down, restarted, plugged in a different keyboard, used a different mouse, unplugged all my periphs, deleted all haxies and their attached files, and it still happened. I unplugged my 2nd monitor, and that seemed to fix it after another restart... but then I plugged in my 2nd monitor again and restarted, and now everything is fine. It's just happening randomly. I'm afraid to restart my Mac now. This is really bizarre!!!
    Someone please help!

    If it's any help, it started right after the following:
    When booted to Vista one night, I noticed ghosting on my nice new iMac screen which kinda irked me. I could see the menubar with the Spotlight menu icon burned into the screen.
    So when I got back into OS X I looked for a menubar hider. I tried Menushade and Menufela, both of which are quite old... probably before any mention of an Intel switch had ever occurred.
    But now I had a problem... no clock. So I went ahead and downloaded Yahoo! Widget Engine (aka Konfabulator). I know, I know... what a crazy workaround just to hide the menubar (this is why it would be GREAT if Apple would build in its own menubar hider option!)
    Anyway, this stuff started happening, so I deleted all of those apps (Menufela, Menushade, and Yahoo Widget Engine) with AppZapper (a great little app).
    Ugh... this is what you get for experimenting with homegrown apps, I guess.
    Anyway, hope that helps.

Maybe you are looking for