Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: TCP Java Socket Server

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default TCP Java Socket Server

    Hi, i wrote this code, but when i connect via telnet, the server gives me no response :/ Please help ?

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.OutputStream;
    import java.net.ServerSocket;
    import java.net.Socket;
    import java.util.logging.Level;
    import java.util.logging.Logger;

    /**
    *
    * @author Jenda
    */

    public class Server extends Thread {
     
        Socket s;
     
        public static void main(String[] args) {
            try {
                ServerSocket ss = new ServerSocket(5222);
                while (true) {
                    Socket s = ss.accept();
                    Server srv = new Server();
                    srv.s = s;
                    srv.start();
                }
            } catch (IOException ex) {
                Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
     
        public void run(Socket s) {
            try {
                InputStream is = s.getInputStream(); //ze site
                OutputStream os = s.getOutputStream(); // to co ja posilam do site
                BufferedReader br = new BufferedReader(new InputStreamReader(is)); //obaleni buffered abych mohl cist po celych radcich
                String str;
                while ((str = br.readLine()) != null) {
                    System.out.println(">> " + str);
                    str = "SRV: " + str + "\n";
                    os.write(str.getBytes());
                }
            } catch (IOException ex) {
                Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
    Last edited by vviston; March 23rd, 2014 at 05:27 AM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,094
    Thanks
    48
    Thanked 2,152 Times in 2,125 Posts

    Default Re: TCP Java Socket Server

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.

    Add calls to the printStackTrace() to the catch blocks so any error messages are shown on the console.

    Also try debugging the code by adding some println() statements that print out messages to show where the code is executing.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: TCP Java Socket Server

    Quote Originally Posted by vviston View Post
    public void run(Socket s) {
    The main point: you are not overriding the run() of Thread.

    But there is also other you should understand and improve ....
    Andrea, www.andbin.net SCJP 5 (91%) SCWCD 5 (94%)

    Useful links for Java beginners My new project Java Examples on Google Code

  4. #4
    Junior Member
    Join Date
    Oct 2011
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: TCP Java Socket Server

    It works now ! The parametr Socket s shouldnt by there

Similar Threads

  1. 2D JAVA GAME TCP SERVER/CLEINT SUPPORT HELP?
    By MdeeY in forum Java Networking
    Replies: 33
    Last Post: April 2nd, 2013, 11:48 AM
  2. TCP/IP java client, c++ server
    By akboyd88 in forum Java Networking
    Replies: 0
    Last Post: March 24th, 2011, 10:46 AM
  3. Replies: 0
    Last Post: February 24th, 2011, 05:31 AM
  4. windowsize TCP socket
    By ra65ma in forum Java Networking
    Replies: 0
    Last Post: January 29th, 2011, 11:58 AM
  5. Decode Binary Packet via TCP Socket
    By maxice in forum Java Networking
    Replies: 0
    Last Post: November 8th, 2010, 02:38 PM