Tech2 Forums - Powered by vBulletin

Server to Server communication help required.

This is a topic on Server to Server communication help required. within the Programmers Corner forums, part of the Technology category; Hi Everybody, I want to establish a communication between Server A to Server B using Server to Server call. Server ...

Results 1 to 3 of 3
  1. #1
    Uber Newbie
    Join Date
    Dec 2004
    Location
    India
    Posts
    47
    Liked
    0 times
    Reputation points
    0

    Server to Server communication help required.

    Hi Everybody,

    I want to establish a communication between Server A to Server B using Server to Server call.

    Server A: is apache and holds a website. I am using cURL to initiate a call and to transmit the data.

    Server B: is based on Java communication protocol.

    Now, my trouble is, even though I am sending the data using cURL with POST method (data is completely secure as its a backend call and cannot be tampered), but Java server is not able to retrieve the data I am sending. Following is the log I received from server which states about the error.

    MerchantsLog merchantsLog=new MerchantsLog();
    ObjectOutputStream objectoutputstream1 = null;
    strHost = request.getRemoteAddr();
    strReferer = request.getHeader("Referer")==null?"":request.getH eader("Referer").trim();
    try
    {
    ObjectInputStream inputStream = new ObjectInputStream(request.getInputStream());
    VERIFICATION_INPUT = (String)inputStream.readObject();
    }


    This is the sting we get from your end. VERIFICATION_INPUT

    Current Exception :
    2012/01/25 15:02:39 | 15:02:39,779 INFO [AllLogs] [----------VERIFICATION_INPUT ENCRYPT----]
    2012/01/25 15:02:39 | 15:02:39,779 ERROR [AllLogs] java.io.StreamCorruptedException: invalid stream header
    2012/01/25 15:02:39 | java.io.StreamCorruptedException: invalid stream header
    2012/01/25 15:02:39 | at java.io.ObjectInputStream.readStreamHeader(ObjectI nputStream.java:764)
    2012/01/25 15:02:39 | at java.io.ObjectInputStream.<init>(ObjectInputStream .java:277)
    2012/01/25 15:02:39 | at org.apache.jsp.TpslMB_jsp._jspService(TpslMB_jsp.j ava:164)
    2012/01/25 15:02:39 | at org.apache.jasper.runtime.HttpJspBase.service(Http JspBase.java:70)
    2012/01/25 15:02:39 | at javax.servlet.http.HttpServlet.service(HttpServlet .java:803)
    2012/01/25 15:02:39 | at org.apache.jasper.servlet.JspServletWrapper.servic e(JspServletWrapper.jav
    I need to get rib of this as this is an urgent matter. Can anybody give me some hints/suggestions to solve this problem. I will be very thankful to you all.

    Awaiting your valuable response.

  2. #2
    Tech2 Members ander's Avatar
    Join Date
    May 2011
    Location
    Bangalore
    Posts
    164
    Liked
    9 times
    Reputation points
    25

    Re: Server to Server communication help required.

    My suggestion: Come up with a custom protocol and use raw sockets. Works like a charm.

    Here's some simplified code that I wrote for a very similar system: A PHP application communicates with a Java server using sockets.
    It's multi-threaded too, and can accept as many connections as you want it to.

    Code:
    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.io.OutputStreamWriter;
    import java.io.PrintWriter;
    import java.net.ServerSocket;
    import java.net.Socket;
    import java.util.ArrayList;
    
    public class CustomServer {
    	public static void main(String[] args) throws Exception {
    		ServerSocket m_ServerSocket = new ServerSocket(54321);
    		while (true) {
    			Socket clientSocket = m_ServerSocket.accept();
    			ClientServiceThread cliThread = new ClientServiceThread(clientSocket);
    			cliThread.start();
    		}
    	}
    }
    
    class ClientServiceThread extends Thread {
    	Socket clientSocket;
    	boolean running = true;
    
    	ClientServiceThread(Socket s) {
    		clientSocket = s;
    	}
    
    	public void run() {
    		try {
    			BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
    			PrintWriter out = new PrintWriter(new OutputStreamWriter(clientSocket.getOutputStream()));
    			while (running) {
    				String clientCommand = in.readLine();
    				if (clientCommand != null) {
    					// Use a switch-case or nested if-else'es with 'clientCommand' to determine what operation needs to be done.
    					// Do the magic
    					// Send output
    
    					// Example:
    					if (clientCommand.equalsIgnoreCase("quit")) {
    						running = false; // Stops the current thread and closes the socket
    					} else if (clientCommand.equalsIgnoreCase("some-other-command")) {
    						// <Insert code for magic here>
    						out.println("<Your output goes here>");
    						out.flush(); // Just to be sure            
    					}
    				}
    			}
    			clientSocket.close();
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    	}
    }
    The original program works perfectly, but I didn't test the code that I posted above, so there might be some bugs left.

    Credits: I borrowed bits and pieces of code and logic from many sources, so I haven't been able to document or give credit to all of them. But I believe I didn't violate the conditions of fair use, so there shouldn't be any serious infringement.
    I'm back, baby! =P

  3. #3
    Tech2 Members ksg91's Avatar
    Join Date
    Mar 2012
    Location
    India
    Posts
    72
    Liked
    6 times
    Reputation points
    12
    Problem has to be with headers you set in cURL, at least I think so.
    My Gear :Nokia N8

Posting Permissions

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