Results 1 to 2 of 2
  1. #1
    suppradyn is offline Member
    Join Date
    Nov 2009
    Posts
    1
    Rep Power
    0

    Default a server program that needs some help :)

    hello guys ;
    i tried to write a simple server to send /receive strings concurrently through the same port .i used a class with thread which implements runnable.but the problem is the class which has runnable thread doesnt recognise socket object that i created in main class.(btw im akind of new at java so the terms i used as talking might be wrong) .i guess it will make sense when u see the program below. if i use different ports for sending and receiving and define new socket objects in each class will solve the problem but i wonder if there s any way to do that through the same port concurently.if i can work it out.id like to share the proper program ( both server and client codes) with u all. pls feel free to ask any question.i hope i could express whats wrong with the program.
    best regards...

    import java.io.IOException;
    import java.io.PrintStream;
    import java.net.InetAddress;
    import java.net.ServerSocket;
    import java.net.Socket;
    import java.util.Scanner;
    import java.io.DataInputStream;
    import java.io.DataInput;


    class MyThreaddd implements Runnable{


    Thread t;
    MyThreaddd () {
    t = new Thread(this,"My thread");
    t.start();
    }
    public void run() {
    for(;;) {
    //to receive date from client
    Scanner sc=new Scanner(s.getInputStream());
    String gelen=sc.nextLine();
    System.out.println("client:"+gelen);
    }
    }
    }

    public class javaserverchat {

    /**
    * @param args
    */
    public static void main(String[] args) {
    // TODO Auto-generated method stub

    try {
    ServerSocket ss=new ServerSocket(25);
    System.out.println("server is ready.."+InetAddress.getLocalHost());
    Socket s=ss.accept();
    System.out.println(s.getInetAddress()+" connected");
    PrintStream pr=new PrintStream(s.getOutputStream());

    pr.println("hello pal im server:) "); //to send string to the client

    new MyThreaddd();

    for(;;) {

    // to send the string we type to the client

    DataInputStream di=new DataInputStream(System.in);
    try {
    String okunan=di.readLine();
    pr.println(okunan); //Client'a mesaj
    //System.out.println(okunan);
    //System.out.flush();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }




    //---------------------------------



    //s.close();

    }
    } catch (IOException e) {
    // TODO Auto-generated catch block
    System.out.println("port mesgul");
    }

    }


    }

  2. #2
    cst
    cst is offline Member
    Join Date
    Nov 2009
    Posts
    7
    Rep Power
    0

    Default

    1. your socket is invisible because it is in a different class, it would be visible if you did something like this:
    Java Code:
    class Chat{
    Socket s;
    class YourThread{}
    }
    So the thread class has to be inside the chat class. There are other methods to do this but i think this is the easiest.
    2 . Dont use such small port numbers like 25 use ports >2000 , because you dont want to use a port which may be taken by system.
    3. I never used scanner for reading lines from , so i dont know if it works well, I rather use BufferedReader, but thats just my preference
    4 There is no point in having two ports, you have a connection on one port, and from that connection you get input and output

Similar Threads

  1. Multithreaded Client/Server Chat program
    By f0ns in forum Threads and Synchronization
    Replies: 3
    Last Post: 10-21-2009, 05:26 PM
  2. smtp server configuration with jboss server
    By vilas_patil in forum Java Servlet
    Replies: 0
    Last Post: 01-05-2009, 01:18 PM
  3. Replies: 2
    Last Post: 12-27-2008, 08:02 PM
  4. Replies: 2
    Last Post: 07-28-2008, 07:32 PM
  5. Replies: 0
    Last Post: 04-04-2008, 02:49 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
  •