Announcement Announcement Module
Collapse
No announcement yet.
connectiong from client socket to a server socket and then to jdbc database? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • connectiong from client socket to a server socket and then to jdbc database?

    Hello,

    I am a total n00b in spring. I have a task to do but i dont know wether spring is the proper technic for this task

    What i have to do is the following:

    connect from a client to a server over sockets to a jdbc database.

    The user on the client side iss adding some data into jtextfields. clicking a button the data should be transfered through a client socket to a server socket. Data Arriving at the serversocket it should be forwarded to the mysql database to make the changes.

    does someone have a sample code or knows a good tutorial with step by step guide how this can be done if spring is suitable for that job at all?

    cheers,

    Bastien

  • #2
    It depends what you mean by "sockets" ... Do you need to access the sockets directly and write your own protocol ? Or do you just need some remoting technology to communicate over the network between your client and your server ?

    In the later case, have a look at http://static.springframework.org/sp.../remoting.html.

    For the database you can have a look at http://static.springframework.org/sp...ddle-tier.html. You can choose between a number of solutions The chapter 5 of Spring MVC tutorial talks about database connections.

    Comment


    • #3
      Originally posted by gehel View Post
      It depends what you mean by "sockets" ... Do you need to access the sockets directly and write your own protocol ? Or do you just need some remoting technology to communicate over the network between your client and your server ?

      In the later case, have a look at http://static.springframework.org/sp.../remoting.html.

      For the database you can have a look at http://static.springframework.org/sp...ddle-tier.html. You can choose between a number of solutions The chapter 5 of Spring MVC tutorial talks about database connections.
      look at this example code here:

      its a client.java and server.java file. starting both files in my eclipse IDE the server is waiting for data getting from the client. I know its a stupid example but it works. Additionally i have to use a mysql database behind the server thats means I start my GUI class on the client pc on the other side is the server waiting for data via the sockets on a certain port. Now i open a socket connection on the client side entering data which the server is waiting for. Forwarding the clients data to the server trough the sockets and then to the mysql database is what i wanna do.

      this is a basic example for the client - server connection and data exchange.

      hope you can advise me now more concretely in which direction i have to go.

      client class:

      Code:
      import java.io.BufferedReader;
      import java.io.BufferedWriter;
      import java.io.IOException;
      import java.io.InputStreamReader;
      import java.io.OutputStreamWriter;
      import java.io.PrintWriter;
      import java.net.InetAddress;
      import java.net.Socket;
      
      public class Client
      {
      
      	public Client()
      	{		
      	}
      	
      	public static void main(String[] args) throws IOException
      	{
      		InetAddress addr = InetAddress.getByName("localhost");		
      		System.out.println("Addresse = " + addr);
      		Socket socket = new Socket(addr , EinfacherServer.PORT);
      		
      		try
      		{
      			System.out.println("socket = " + socket);
      			BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
      			
      			// Output is automatically flushed by PrintWRiter			
      			PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true);	
      		    
      		    for(int i = 0; i < 10; i++)
      		    {
      		    	out.println("Zeile: " + i);
      		    	String str = in.readLine();
      		    	System.out.println(str);
      		    }	
      			out.println("END");
      		}
      		finally 
      		{
      			System.out.println("schliesse...");
      			socket.close();
      		}
      	}
      }
      server class:

      import java.io.BufferedReader;
      import java.io.BufferedWriter;
      import java.io.IOException;
      import java.io.InputStreamReader;
      import java.io.OutputStreamWriter;
      import java.io.PrintWriter;
      import java.net.ServerSocket;
      import java.net.Socket;


      Code:
      public class Server
      {
      
      	 // MySql port is 3306 !!!
      	
      	 public static final int PORT = 8080; // for socket connection
      	 
      	
      	public static void main(String[] args)throws IOException
      	{
      		ServerSocket s = new ServerSocket(PORT);
      		System.out.println("Started: " + s);
      		
      		
      		try
      		{
      			// Wait for connection of client
      			
      			Socket sock = s.accept();
      			
      			try
      			{
      				System.out.println("Verbindung aufgenommen: " + sock);
      				
      				BufferedReader in = new BufferedReader(new InputStreamReader(sock.getInputStream()));
      				
      				// Die Ausgabe wird erzwungen, auch wenn der buffer nicht volist , wegen true
      				
      				PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(sock.getOutputStream())),true);
      				
      				while(true)
      				{
      					String str = in.readLine();
      					if(str.equals("END")) break;
      					
      					System.out.println("Echoing: " + str);
      					out.println(str);				
      				}
      				// Schliesse beiden sockets client und server socket
      				
      			}  
      			finally
      			{ 
      				System.out.println("schliesse...");
      				sock.close();				
      			}			
      		}
      		finally		
      		{
      			s.close();
      		}
      
      	}
      
      }

      Comment


      • #4
        Difficult to give you a good advice with very vague requirements. It depends a lot on the kind of messages you need to send between the client and the server, if the application should be just an exercise, what size it will be ...

        If I had to do it myself I'd do it with Spring-remoting and Hessian ... because I am comfortable with these ...

        Comment


        • #5
          Originally posted by gehel View Post
          Difficult to give you a good advice with very vague requirements. It depends a lot on the kind of messages you need to send between the client and the server, if the application should be just an exercise, what size it will be ...

          If I had to do it myself I'd do it with Spring-remoting and Hessian ... because I am comfortable with these ...
          but remoting is with RMI which i should avoid and the messages are plain strings entered in some jtextfields of a GUI.

          Comment


          • #6
            Originally posted by Bastien View Post
            but remoting is with RMI which i should avoid and the messages are plain strings entered in some jtextfields of a GUI.
            I was talking about Hessian, not RMI. You can think about Hessian as closer to WebService, but with a lighter weight binary protocol. It is supposed to be more efficient than SOAP ...

            Of course, it's your choice, but I think it's a good idea to use a standard protocol, which will be able to scale if your messages become more complex. It is also easier to have a clear demarcation between the services themselves and the remoting implementation if you use a standard. And using Spring with Hessian is dead simple, probably much easier than implementing your own protocol, even if your messages are simple.

            Once more, without having a clear picture of your requirement, it is difficult to give you a good answer ...

            Comment

            Working...
            X