Results 1 to 4 of 4
  1. #1
    kevinn205 is offline Member
    Join Date
    Nov 2011
    Posts
    65
    Rep Power
    0

    Default Simple Server Question

    Hellow, I'm using this server to contact other computers on my network but i'm not sure how it detemines which computer to send it to

    Does anyone know where the code is that determines where the message is sent to, an example of a message from this server to a ip would be great but I think i'll be able to figure it out if i know where it gets their ip

    thanks in advance!

    Java Code:
    //The server code Server.java:      
     import java.io.*;  
     import java.net.*;  
     import java.util.*;   
     /**   
      * This is to help people to write Client server application   
      *  I tried to make it as simple as possible... the client connect to the server  
      *  the client send a String to the server the server returns it in UPPERCASE thats all  
      */ 
    
     public class Server {    
         // the socket used by the server  
    
         private ServerSocket serverSocket;  
         // server constructor  
         Server(int port) {  
             /* create socket server and wait for connection requests */ 
    
             try  
             {  
                 serverSocket = new ServerSocket(port);  
                 System.out.println("Server waiting for client on port " + serverSocket.getLocalPort());  
                 while(true)   
                 {  
                     Socket socket = serverSocket.accept();  // accept connection  
                     System.out.println("New client asked for a connection");  
                     TcpThread t = new TcpThread(socket);    // make a thread of it  
                     System.out.println("Starting a thread for a new Client");  
                     t.start();  
                 }  
             }  
             catch (IOException e) {  
                 System.out.println("Exception on new ServerSocket: " + e);  
             }  
         }             
     //  you must "run" server to have the server run as a console application  
         public static void main(String[] arg) {  
             // start server on port 1500  
             new Server(1500);  
         }         
         /** One instance of this thread will run for each client */ 
        class TcpThread extends Thread {  
             // the socket where to listen/talk  
             Socket socket;  
             ObjectInputStream Sinput;  
             ObjectOutputStream Soutput;             
             TcpThread(Socket socket) {  
                 this.socket = socket;  
             }  
             public void run() {  
                 /* Creating both Data Stream */ 
                 System.out.println("Thread trying to create Object Input/Output Streams");  
                 try 
                 {  
                     // create output  
                     Soutput = new ObjectOutputStream(socket.getOutputStream());  
                     Soutput.flush();  
                     Sinput  = new ObjectInputStream(socket.getInputStream());  
                 }  
    
                 catch (IOException e) {  
                     System.out.println("Exception creating new Input/output Streams: " + e);  
                     return; 
                 }  
                 System.out.println("Thread waiting for a String from the Client");  
                 // read a String (which is an object)  
    
                 try {  
                     String str = (String) Sinput.readObject();  
                     Soutput.writeObject("hellow");
                   
                     
                     Soutput.flush();  
                    // jtable.GetModel();
                 }  
                 catch (IOException e) {  
                     System.out.println("Exception reading/writing  Streams: " + e);  
                     return;               
                 }  
                 // will surely not happen with a String  
                 catch (ClassNotFoundException o) {                
                 }  
                 finally {  
                     try {  
                         Soutput.close();  
                         Sinput.close();  
                     }  
                    catch (Exception e) {                     
                     }  
                 }  
            }  
         }  
     }

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default Re: Simple Server Question

    Have you read the tutorial at Oracle?
    Lesson: All About Sockets (The Java™ Tutorials > Custom Networking)
    The server only knows of the client when a client attempts to connect

  3. #3
    kevinn205 is offline Member
    Join Date
    Nov 2011
    Posts
    65
    Rep Power
    0

    Default Re: Simple Server Question

    I have, but I'm moderatly confused, I'm trying to manage which clients recieve the message, it seems to me like my current program only returns the request to the requester even if others are connected; I figured perhaps I can use some kind of send all or find out how to manage connections to send to all

    perhaps it would allow me to check if a connection to a ip is established and if it is to send a message?

  4. #4
    kevinn205 is offline Member
    Join Date
    Nov 2011
    Posts
    65
    Rep Power
    0

    Default Re: Simple Server Question

    Sigh heres the snippet I was looking for...

    hope it helps someones else

    Socket socket = new Socket("192.168.1.21", port);

Similar Threads

  1. simple FTP server and FTP client
    By simontkk2005 in forum Networking
    Replies: 2
    Last Post: 11-17-2010, 10:38 AM
  2. Simple server-client
    By DC200 in forum New To Java
    Replies: 6
    Last Post: 12-09-2009, 05:13 PM
  3. simple chat server
    By sari in forum New To Java
    Replies: 0
    Last Post: 02-06-2009, 02:30 AM
  4. A simple multithreaded server
    By Java Tip in forum java.net
    Replies: 0
    Last Post: 04-07-2008, 08:15 PM
  5. A Simple Web Server
    By Java Tip in forum java.net
    Replies: 0
    Last Post: 04-07-2008, 08:04 PM

Posting Permissions

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