Thread: Simple communication protocol

Results 1 to 2 of 2
  1. #1 Simple communication protocol 
    Donator


    Join Date
    Feb 2014
    Age
    26
    Posts
    161
    Thanks given
    261
    Thanks received
    62
    Rep Power
    100
    I'm designing a communications system for my project. However, I'm having some trouble handling packets. My communications system is written in java.net using ServerSocket, Socket, BufferedReader, and PrintWriter.

    Currently, my system listens for packets every 500ms. It handles the login packet fine but it fails to handle the post-login packets. Every 500ms, I need my server to handle login and post-login packets. How would I design a method that would detect and process post-login packets? The login packet consists of the handshake code, username, and password. Post-login packets consist of the handshake code, user id, event id and other event parameters.

    This is my server sided decoder
    Code:
    	protected void decode(Socket server) throws Exception {
    		final BufferedReader reader = new BufferedReader(new InputStreamReader(server.getInputStream()));
    		final int handshake = Integer.parseInt(reader.readLine());
    		if (handshake == LOGIN) {
    			final Session session = new LoginSession(server, reader);
    			session.read(-1);
    		} else if (handshake == POST_LOGIN) {
    			final int index = Integer.parseInt(reader.readLine());
    			final int opcode = Integer.parseInt(reader.readLine());
    			final Session session = store.get(index).getSession();
    			session.reader = reader;
    			session.read(opcode);
    		}
    
    	}
    
    	@Override
    	public void run() {
    		Socket server;
    		try {
    			server = serverSocket.accept();
    			server.setTcpNoDelay(false);
    			decode(server);
    		} catch (Exception e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		
    	}
    These are my incoming packets from the client to the server
    Code:
    	public void sendLogin() {
    		write.write("100");//login handshake
    		write.write('\n');
    		write.write("raees2");//username
    		write.write('\n');
    		write.write("LOL2");//password
    		write.write('\n');
    		write.flush();
    	}
    	
    	public void sendPrintId() throws NumberFormatException, IOException {
    		final BufferedReader reader = new BufferedReader(new InputStreamReader(client.getInputStream()));
    		final int id = Integer.parseInt(reader.readLine());
    		System.out.println(id);
    		
    		write.write("101");//post login handshake
    		write.write('\n');
    		write.write(Integer.toString(id));//user id
    		write.write('\n');
    		write.write(Integer.toString(0));//event id
    		write.write('\n');
    		write.write(Integer.toString(10003));//event parameter
    		write.write('\n');
    		write.flush();
    	}
    	
    	public void sendDisconnection() throws NumberFormatException, IOException {
    		final BufferedReader reader = new BufferedReader(new InputStreamReader(client.getInputStream()));
    		final int id = Integer.parseInt(reader.readLine());
    		System.out.println(id);
    		
    		write.write("101");//post-login handshake
    		write.write('\n');
    		write.write(Integer.toString(id));//user id
    		write.write('\n');
    		write.write(Integer.toString(1));//event id
    		write.write('\n');
    		write.flush();
    	}
    Reply With Quote  
     

  2. #2  
    Donator


    Join Date
    Feb 2014
    Age
    26
    Posts
    161
    Thanks given
    261
    Thanks received
    62
    Rep Power
    100
    fixed mod close
    Reply With Quote  
     


Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)


User Tag List

Similar Threads

  1. Crunchy Background ~ Simple
    By Inside Sin in forum Tutorials
    Replies: 8
    Last Post: 06-19-2008, 05:53 PM
  2. Simple Grunge Tut
    By rum0 in forum Tutorials
    Replies: 9
    Last Post: 11-11-2007, 04:17 AM
  3. Replies: 7
    Last Post: 05-16-2007, 11:54 PM
  4. [Simple TuT] Fixing Dragonhide Bodies (Normal)
    By Zamorak Zxt in forum Tutorials
    Replies: 0
    Last Post: 05-10-2007, 05:00 AM
  5. Simple means <3?
    By Beh3moth2 in forum Showcase
    Replies: 6
    Last Post: 04-28-2007, 03:01 PM
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •