Socket programming to transmit message over TCP/IP
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Socket programming to transmit message over TCP/IP

  1. #1
    Join Date
    Sep 2013
    Posts
    1

    Socket programming to transmit message over TCP/IP

    Here is the scenario :

    Client Side : pack and send message to server using TCP/IP
    Receive the response message from server
    unpack the Response message

    ServerSide :
    Receive the packed message sent by Client using TCP/IP
    pack and Send a Response message to Client


    Server is able to receive the message sent by client and will be able to reach the code block to send response only when i comment the receive_PackedResponseData.readLine().getBytes()) in client code (But i'm not able to see the response on client side.). If its uncommented then server will not even reach the block of code to send response.

    I'm not able to understand whats wrong in it. Looking for your help.

    Client Code :
    Code:
                            BufferedOutputStream outStream = null;
    			BufferedReader receive_PackedResponseData = null;			
    
    			  /**  Construct Request Message */
                            GenericPackager packager = new GenericPackager("basic.xml");
                            ISOMsg isoMsg = new ISOMsg();
    			isoMsg.setPackager(packager);
                            isoMsg.setMTI("0300");
    			//set all the required Data Elements
    
                           byte[]  send_PackedRequestData = isoMsg.pack();
    
                           Socket connection = new Socket("localhost", 8180);
    			if (connection.isConnected()) {
    				outStream = new BufferedOutputStream(
    						connection.getOutputStream());
    				outStream.write(send_PackedRequestData);
    				outStream.flush();
    				System.out.println("-----Request sent to server---");
    				System.out.println(ISOUtil.hexString(send_PackedRequestData));
    			}
    
    			/** Receive Response */
    
    			if (connection.isConnected()) {
    				receive_PackedResponseData = new BufferedReader(
    						new InputStreamReader(connection.getInputStream()));
    				System.out.println("-- RESPONSE recieved---");
    				System.out.println(receive_PackedResponseData);
    				// System.out.println("Response : "+ISOUtil.hexString(receive_PackedResponseData.readLine().getBytes()));				
    			}
    
    			receive_PackedResponseData.close();
    			outStream.close();
    			connection.close();

    Server Code :

    Code:
     BufferedReader receive_PackedRequestData =null;
    		BufferedOutputStream outStream=null;
            
    		/** Establish connection */
    		ServerSocket Server = new ServerSocket(port);
    		System.out.println("TCPServer Waiting for client on port " + port);
    		Socket connection = Server.accept();
    			
    
    
    		/** Receive Request */
    		if (connection.isConnected()) {
    			 receive_PackedRequestData = new BufferedReader(new InputStreamReader(connection.getInputStream()));
    			fromclient = receive_PackedRequestData.readLine();
    			
    			System.out.println("--- Request Received from Client--- ");
    			System.out.println( ISOUtil.hexString(fromclient.getBytes()));
    		}
    
    		/** Send the response */
    		if (connection.isConnected()) {
    			//Construct Response Message
    			isoMsg=new ISOMsg();
    			GenericPackager packager = new GenericPackager("basic.xml");
    			isoMsg.setPackager(packager);
    			isoMsg.setMTI("0310"); 
    			byte[] send_PackedResponseData = msg.pack();
    			
    			outStream = new BufferedOutputStream(connection.getOutputStream());
    			outStream.write(send_PackedResponseData);
    			outStream.flush();
    			
    			System.out.println("-------RESPONSE sent---------");
    			System.out.println(ISOUtil.hexString(send_PackedResponseData));
    		}
    		
    		receive_PackedRequestData.close();
    		outStream.close();
    		connection.close();
    Note : I'm running servercode and client code in seperate instance of eclipse on the same machine.

  2. #2
    Join Date
    Jun 1999
    Location
    SW Missouri
    Posts
    3,429

    Re: Socket programming to transmit message over TCP/IP

    Could you make complete classes out of the code so it can be compiled and executed for testing?

    For testing I use a main() method like this that calls the server and client on their own threads:
    Code:
       public static void main(final String[] args) {    //<<<<<<<<<<<<<<<<<<<
    
          Thread t1 = new Thread(new Runnable() {
             public void run() {
                ClassOnServer.main(args);
             }
          });
          t1.start();
    
          try{Thread.sleep(100);}catch(Exception x){}
    
          Thread t2 = new Thread(new Runnable() {
             public void run() {
                ClassOnClient.main(args);
             }
          });
          t2.start();
    
    
          // wait and exit
          try{Thread.sleep(5000);}catch(Exception x){}
          System.out.println("Exiting main");
          System.exit(0);
       }  //  end main
    Norm

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center