LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-24-2014, 10:30 PM   #1
kedarp
Member
 
Registered: Jul 2012
Distribution: Ubuntu
Posts: 198
Blog Entries: 3

Rep: Reputation: 23
Problem with simple Java network program


Hello,
I was just trying a simple program to get reply from the Server. I need some help with it.

Here is the Server code -
Code:
package simpleprograms;

import java.net.Socket;
import java.net.ServerSocket;
import java.io.PrintWriter;

public class ListeningServer {

    public static void main(String[] args) {
        try {
            //InetSocketAddress addr=new InetSocketAddress(7);
            ServerSocket servsock = new ServerSocket(7800);
            Socket sock;
            PrintWriter pw;
            
            if(servsock.isBound()) {
                System.out.println("Server at " + servsock.getInetAddress() + 
                        " is listening on port " + servsock.getLocalPort());
            }
            else {
                System.out.println("Bind Error");
                //exit(1);
            }
            while (true) {
                sock = servsock.accept();
                pw=new PrintWriter(sock.getOutputStream());
                pw.println("Hello");
                sock.close();
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
Here is the client program -
Code:
package simpleprograms;

import java.io.DataInputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;

public class PingServer {

    public static void main(String[] args) {
        try {
            Socket t = new Socket("127.0.0.1", 7800);
            DataInputStream dis = new DataInputStream(t.getInputStream());
            byte[] b = new byte[1024];

            dis.read(b);
            String str = new String(b);
            System.out.println(str);
            if (str.equals("Hello")) {
                System.out.println("Alive");
            } else {
                System.out.println("Dead");
            }
            t.close();
        }
        catch(UnknownHostException ex) {
            System.out.println(ex.toString());
        }
        catch(IOException ex) {
            System.out.println(ex.toString());
        }
        catch (Exception ex) {
            //System.out.println(ex.toString());
            ex.toString();
        }
    }
}
When I run the Server and the Client, I get the O/P from the Server as -
Dead
 
Old 11-25-2014, 03:27 PM   #2
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
There is at least a couple of bugs in this code.

The server fails to flush its output and the client test is wrong..
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Why does this simple Java program work correctly under Windows but not Fedora 10? arashi256 Linux - Newbie 7 09-05-2014 08:22 PM
Help needed to run simple java program in linux pinga123 Linux - Newbie 7 05-06-2010 06:25 AM
unrecognized class file version : when running simple Java program yuubouna Linux - Newbie 1 11-27-2008 11:25 PM
How do I program a simple network messenger? creatureofthedark Programming 15 01-27-2007 08:02 AM
java, simple program help needed.. marlor Programming 1 11-04-2004 02:40 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:17 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration