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.


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

    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;
    } 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";
    } 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

    Default Re: TCP Java Socket Server

    Please edit your post and wrap your code with code tags:
    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

    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

    Default Re: TCP Java Socket Server

    It works now ! The parametr Socket s shouldnt by there

Similar Threads

    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