Solved

Client Server Application

Posted on 1998-09-01
3
156 Views
Last Modified: 2010-03-30
Can someome give me the code for a client and server application using sockets? I did this in C but not java yet.
0
Comment
Question by:joe7
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 3

Accepted Solution

by:
mjenkins earned 200 total points
ID: 1223136
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
 
LVL 3

Expert Comment

by:mjenkins
ID: 1223137
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
 

Author Comment

by:joe7
ID: 1223138
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Suggested Courses

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question