Page 2 of 2 FirstFirst 12
Results 21 to 26 of 26
  1. #21
    charleyjoyce is offline Member
    Join Date
    Jan 2011
    Posts
    18
    Rep Power
    0

    Default

    here's an example of a client /server
    Java Code:
    import java.io.*;
    import java.net.*;
    public class Client {
            private static Socket client;
        private static ObjectInputStream input;
        private static ObjectOutputStream output;
            public static void main(String[] args) throws IOException, EOFException{
                            System.out.println("connecting to server..");
                            client = new Socket(InetAddress.getByName("127.0.0.1"), 12345);
                            System.out.println("connected to : " + client.getInetAddress().getHostName() );
                            output = new ObjectOutputStream( client.getOutputStream() );
                            output.flush();
                            output.writeObject( args[0] );
                            output.flush();
                            output.close();
                            client.close();
            }
    }
    Java Code:
    import java.net.*;
    import java.io.*;
    public class Server {
        private static ObjectInputStream input;
            private static ObjectOutputStream output;
            private static ServerSocket server;
            private static Socket connection;
            public static void main(String[] args) throws Exception{
                            server =  new ServerSocket(12345,100);
                            while (true){
                                            System.out.println("waiting for connection..");
                                            connection = server.accept();
                                            System.out.println("Connection received: " + connection.getInetAddress().getHostName());
                                            output = new ObjectOutputStream( connection.getOutputStream() );
                                            output.flush();
                                            input = new ObjectInputStream(connection.getInputStream() );
                                            System.out.println("Recv'd stream");
                                            String message = (String) input.readObject() ;
                                            System.out.println(message);
                                            output.close();
                                            input.close();
                                            connection.close();
                            }
            }
    }

  2. #22
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,051
    Blog Entries
    7
    Rep Power
    23

    Default

    Quote Originally Posted by charleyjoyce View Post
    here's an example of a client /server
    Please don't give it all away and leave some work to be done for the OP; spoonfeeding is considered not done in this forum and the OP doesn't learn anything when you supply boilerplate code.

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

  3. #23
    krishforever is offline Member
    Join Date
    Jan 2011
    Posts
    19
    Rep Power
    0

    Default

    thanks for everyone.

  4. #24
    user0 is offline Senior Member
    Join Date
    Dec 2010
    Posts
    100
    Rep Power
    0

    Default

    Hi Krish - its advisable that you read some client server tutorials just as Fubarable mentioned in the earlier post. JosAH's code base is also a good starting point.

    I learned socket programming using the Java tutorials, so here is a link to them for your reference: Reading from and Writing to a Socket (The Java™ Tutorials > Custom Networking > All About Sockets)

    Have a thorough read through and see if it helps in your solution. If not, post back here, lets try to debug your code.

    Best,
    --user0--

  5. #25
    danborgir is offline Member
    Join Date
    Jan 2011
    Posts
    26
    Rep Power
    0

    Default Simple Server & Client

    Server:

    Java Code:
    import java.net.*;
    import java.io.*;
    
    public class SimpleServer {
    public static void main(String args[]) {
    
    ServerSocket s = null;
    Socket s1;
    String sendString ="Hello network world!!!!";
    OutputStream os;
    DataOutputStream dos;
    
    try {
    s = new ServerSocket(5432);
    } catch (IOException e) { }
    while (true) {
    try {
    // wait here and listen for a connection
    s1 = s.accept();
    //Get a communication stream associated with the socket
    os = s1.getOutputStream();
    dos = new DataOutputStream (os);
    //Send your string! (UTF provides machine independence)
    dos.writeUTF(sendString);
    // close the connection, but not the server socket
    dos.close();
    os.close();
    s1.close();
    } catch (IOException e) {
    // ignore 
      } } }
    }
    Client:
    Java Code:
    import java.net.*;
    import java.io.*;
    
    public class SimpleClient {
    public static void main(String args[]) throws IOException {
    
    Socket s1;
    InputStream s1In;
    DataInputStream dis;
    
    s1 = new Socket("127.0.0.1", 5432);
    s1In = s1.getInputStream();
    dis = new DataInputStream(s1In);
    String st = new String(dis.readUTF( ));
    System.out.println(st);
    
    dis.close(); s1In.close(); s1.close();
       }
     }
    Hope that helps as a template.

  6. #26
    krishforever is offline Member
    Join Date
    Jan 2011
    Posts
    19
    Rep Power
    0

    Default

    thanks for everyone. i could complete my assignment base on your support.thanks again.

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Java Server/C Client
    By FallenBlade in forum Networking
    Replies: 13
    Last Post: 03-11-2011, 12:22 AM
  2. Replies: 2
    Last Post: 09-24-2010, 11:31 AM
  3. java server and c client ?????????
    By biebo in forum Networking
    Replies: 7
    Last Post: 07-24-2010, 04:35 AM
  4. Java Message Server/Client help
    By sari in forum Networking
    Replies: 3
    Last Post: 03-21-2010, 12:17 AM
  5. Creating an IRC client in Java
    By VeasMKII in forum New To Java
    Replies: 0
    Last Post: 06-17-2009, 09:19 AM

Posting Permissions

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