Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 166
  • Last Modified:

Client Server Application

Can someome give me the code for a client and server application using sockets? I did this in C but not java yet.
0
joe7
Asked:
joe7
  • 2
1 Solution
 
mjenkinsCommented:
public class Client
{
        Socket s;
 
        public static void main( String args[] )
        {
                Client c = new Client();
                c.sendObject();
                c.close();
                System.out.println( "Done Sending" );
        }
 
        public Client()
        {
                try
                {
                        s = new Socket( "localhost", 10888 );
                }
                catch( Exception e )
                {
                        e.printStackTrace();
                        System.exit(0);
                }
        }
 
        public void sendObject()
        {
                try
                {
                        OutputStream out = s.getOutputStream();
                        ObjectOutputStream output = new ObjectOutputStream( out );
                        output.writeObject(new Character('a'));
                        output.writeObject(new Long(12345));
                        output.writeObject(new Double(123.456));
                        output.flush();
                        output.close();
                }
                catch( IOException e )
                {
                        e.printStackTrace();
                }
        }
 
        public void close()
        {
                try
                {
                        s.close();
                }
                catch( Exception e )
                {
                        e.printStackTrace();
                        System.exit(0);
                }
        }
}


import java.io.*;
 
public class Server
{
    ServerSocket s;
 
    public static void main( String args[] )
    {
        Server c = new Server();
        c.recvObject();
        System.out.println( "Done Recieving" );
    }
 
    public Server()
    {
        try
        {
            s = new ServerSocket( 10888, 10 );
        }
        catch( Exception e )
        {
            e.printStackTrace();
            System.exit(0);
        }
    }
 
    public void recvObject()
    {
 
        try
        {
            Socket s1 = s.accept();
            InputStream in = s1.getInputStream();
            ObjectInputStream input = new ObjectInputStream( in );
            Character c = (Character)input.readObject();
            Long l = (Long)input.readObject();
            Double d = (Double)input.readObject();
            input.close();
            s1.close();
            System.out.println( "SERVER RECIEVED: " + c + " " + l + " " + d );
        }
        catch( Exception e )
        {
            e.printStackTrace();
        }
    }
}


0
 
mjenkinsCommented:
The above is a simple Client and Server Application pair that just serializes a couple of objects and shoots them across the socket connection. Substitute your stuff for sendObject and recvObject. If you've done this in "C" then this should be sufficient. If it isn't, let me know and I'll fill in the blanks for you.
0
 
joe7Author Commented:
That's a lot easier than doing it in C! I should have done less points, I thought it was harder to do!
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now