Results 1 to 6 of 6

Thread: Problem with Sockets and Protocols

  1. #1
    Join Date
    Dec 2009
    Posts
    211

    Problem with Sockets and Protocols

    Hello,
    For my applications, networks, client side, I often thread that listens for messages from the server and then I make a code like this. Here is the code
    Code:
    while(true)
    {
    	String m = socket.recoitMessage();
    	if(m.equals("Connect")
    	{
    		//...
    	}else if(m.equals("disconnected")
    	{
    		//....
    	}else if(m.equals("begins")
    	{
    		//....
    	}else if(m.equals("finished")
    	{
    		//....
    	} / / etc. ....
    }
    But I think I have a problem with the Socket and the Protocol which I use.

  2. #2
    Join Date
    Nov 2009
    Posts
    343

    Re: Problem with Sockets and Protocols

    Hello,
    Just check out the following code.
    Code:
    class Processing {
      Public static Final String ACTION_CONNECTER = "Connect";
      Public static Final String ACTION_DECONNECTER = "disconnect";
      Public static Final String ACTION_COMMENCER = "start";
      Public static Final String ACTION_FINIR = "Finally";
    
      Map mp = new HashMap();
     
      Public Processing() {
        / * It instantiates each answer * /
        mp.could(ACTION_CONNECTER, new Connect());
        mp.could(ACTION_DECONNECTER, new Log out());
        mp.could(ACTION_COMMENCER, new Begin());
        mp.could(ACTION_FINIR, new Finish());
      }
     
      / * We treat mp * /
      Public Process() {
        Context ctx = new Context(ctx);
     
        while (true) {
          String mg = socket.recoitMessage();
          Response res = (Response)mp.get(mg);
          if (res! = null) {
            res.Process(ctx);
          }
        }
      }

  3. #3
    Join Date
    Nov 2009
    Posts
    583

    Re: Problem with Sockets and Protocols

    Hello,
    Instead of Strings and if / else I would use (if you can encode 1.5) enums.
    Code:
    enum PROTOCOL {CONNECT, QUIT, SEND, RECEIVE ...}
    and in your loop you use one:
    Code:
    switch (Action) {
      box CONNECT: / / On connect
        break;
      box QUIT: / / We quit
        break;
      ...
    }
    Well it does not improve the code of the masses, but it avoids the if / else repeat with string.

  4. #4
    Join Date
    Nov 2009
    Posts
    347

    Re: Problem with Sockets and Protocols

    Hello,
    If it is to decode the message, there is generally a "factory" which general object corresponding to the message. And in the factory there either
    Code:
    if(msg.equals("Connect")
    {
    	return new Connect(buf);
    }
    else if(msg.equals("disconnected")
    ...
    Or the solution (not so heavy that it anyway because he is a class type of message). The pattern "state" is more for managing the state of the connection.

  5. #5
    Join Date
    Nov 2009
    Posts
    583

    Re: Problem with Sockets and Protocols

    Hello,
    I have a code with me, if you need you can have a try to it. Here is the code
    Code:
    while(true)
    {
    	String str = socket.recoitMessage();
    treat( str );
    }
     
    void treat( String action ) throws Exception
    {
    	Method m = this.getClass().getMethod( Action null );
    m.invoke( this, null );
    }
     
    void Login()
    {
    	System.out.System.out.println( "Connect" );
    }
     
    void Logout()
    {
    	System.out.System.out.println( "disconnect" );
    }
    I think if you follow this logic then your code will be more productive.

  6. #6
    Join Date
    Nov 2009
    Posts
    518

    Re: Problem with Sockets and Protocols

    Hello,
    Just try the following code, it is not the complete code, but still you can set the basic idea.
    Code:
    try
    {
    
    System.out.println("Creating datagram");
    DatagramSocket clsc = new DatagramSocket();
    
    InetAddress ipadd =
    InetAddress.getByName("localhost");
    byte[] sndt;
    byte[] rcdt = new byte[1024];
    String sentence ="TEST";
    sndt = sentence.getBytes();
    DatagramPacket sendPacket =
    new DatagramPacket(sndt, sndt.length,
    ipadd, 9998);
    clsc.send(sendPacket);
    
    }

Similar Threads

  1. First mainboard created with two sockets
    By Anna_relic in forum Motherboard Processor & RAM
    Replies: 2
    Last Post: 04-01-2012, 07:41 PM
  2. Overclocking not a problem with the LGA2011 Sockets
    By PuppETR in forum Motherboard Processor & RAM
    Replies: 3
    Last Post: 15-12-2010, 11:44 AM
  3. AM2+ vs AM3 sockets
    By Xymaya in forum Hardware Peripherals
    Replies: 6
    Last Post: 21-11-2010, 05:39 AM
  4. Binary sockets in C#
    By CheckMeNot in forum Software Development
    Replies: 3
    Last Post: 18-11-2009, 11:02 AM
  5. Sockets in python
    By Benito in forum Software Development
    Replies: 2
    Last Post: 19-08-2008, 01:15 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,438,856.10573 seconds with 16 queries