question regarding Echoclient program - java

I am always getting the message Don't know about host: taranis. while running echoclient program. here is the program below
import java.io.*;
import java.net.*;
public class EchoClient {
public static void main(String[] args) throws IOException {
Socket echoSocket = null;
PrintWriter out = null;
BufferedReader in = null;
try {
echoSocket = new Socket("taranis",3218);
out = new PrintWriter(echoSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(
echoSocket.getInputStream()));
} catch (UnknownHostException e) {
System.err.println("Don't know about host: taranis.");
System.exit(1);
} catch (IOException e) {
System.err.println("Couldn't get I/O for "
+ "the connection to: taranis.");
System.exit(1);
}
BufferedReader stdIn = new BufferedReader(
new InputStreamReader(System.in));
String userInput;
while ((userInput = stdIn.readLine()) != null) {
out.println(userInput);
System.out.println("echo: " + in.readLine());
}
out.close();
in.close();
stdIn.close();
echoSocket.close();
}
}

You need to use a valid host name, or a valid IP of your server (assuming you have one) when you initialize your socket (new Socket("taranis",3218) ). It is great to take those tutorials (as pointed by icktoofay), but especially when it comes to networking, you have to make sure you have the matching application running on the other side, and that the parameters match it. IP and port usually change from machine to machine and from application to application.

Related

how to run java server socket program (class file) in openshift

I want to run a socket program in my openshift server.
my action_hooks/start:-
#!/bin/bash
# The logic to start up your application should be put in this
# script. The application will work only if it binds to
# $OPENSHIFT_DIY_IP:8080
#nohup $OPENSHIFT_REPO_DIR/diy/testrubyserver.rb $OPENSHIFT_DIY_IP $OPENSHIFT_REPO_DIR/diy |& /usr/bin/logshifter -tag diy &
java -cp $OPENSHIFT_REPO_DIR/diy/EchoServer
my server program:-
import java.net.*;
import java.io.*;
public class EchoServer
{
public static void main(String[] args) throws IOException
{
ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket();
serverSocket.bind(new InetSocketAddress("xxx.xxx.xx.xx",8080));
}
catch (IOException e)
{
System.err.println("Could not listen on port: 8080.");
System.exit(1);
}
Socket clientSocket = null;
System.out.println ("Waiting for connection.....");
try {
clientSocket = serverSocket.accept();
}
catch (IOException e)
{
System.err.println("Accept failed.");
System.exit(1);
}
System.out.println ("Connection successful");
System.out.println ("Waiting for input.....");
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(),
true);
BufferedReader in = new BufferedReader(
new InputStreamReader( clientSocket.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
{
System.out.println ("Server: " + inputLine);
out.println(inputLine);
if (inputLine.equals("Bye."))
break;
}
out.close();
in.close();
clientSocket.close();
serverSocket.close();
}
}
my client program:-
import java.io.*;
import java.net.*;
public class EchoClient {
public static void main(String[] args) throws IOException {
String serverHostname = new String ("xxxxxxxxxxx.rhcloud.com");
if (args.length > 0)
serverHostname = args[0];
System.out.println ("Attemping to connect to host " +
serverHostname + " on port 8080.");
Socket echoSocket = null;
PrintWriter out = null;
BufferedReader in = null;
try {
echoSocket = new Socket(serverHostname, 8080);
out = new PrintWriter(echoSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(
echoSocket.getInputStream()));
} catch (UnknownHostException e) {
System.err.println("Don't know about host: " + serverHostname);
System.exit(1);
} catch (IOException e) {
System.err.println("Couldn't get I/O for "
+ "the connection to: " + serverHostname);
System.exit(1);
}
BufferedReader stdIn = new BufferedReader(
new InputStreamReader(System.in));
String userInput;
System.out.print ("input: ");
while ((userInput = stdIn.readLine()) != null) {
out.println(userInput);
System.out.println("echo: " + in.readLine());
System.out.print ("input: ");
}
out.close();
in.close();
stdIn.close();
echoSocket.close();
}
}
my log file "diy.log"
[2015-12-22 11:59:03] INFO WEBrick 1.3.1
[2015-12-22 11:59:03] INFO ruby 1.8.7 (2013-06-27) [x86_64-linux]
[2015-12-22 11:59:03] INFO WEBrick::HTTPServer#start: pid=44992 port=8080
[2015-12-22 12:05:36] INFO going to shutdown ...
[2015-12-22 12:05:36] INFO WEBrick::HTTPServer#start done.
I don't know the server program automatically start or not but when i run server program through ssh, the program is running but not responding to client.
It seems you are trying to connect to port 8080 with your client:
echoSocket = new Socket(serverHostname, 8080);
The server still needs to be bound to 8080 to be externally reachable, but clients should connect to 80 or 8000 (websockets) actually. See this diagram for details on how to route requests on OpenShift.
Check the terminal output when pushing code, to see whether your action hook that is launching the server works. Make sure to have the hook file executable.

Java TCPSocket breaks after Client closes connection [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I am very new to programming, especially to socket programming. I tried to figure out how communication works in real (not in all my books), but ran immediately into my first problem with the downloaded SimpleEchoServer example. Communicationflow works but when the Clientsocket closes his connection without sending a specific string, my serversocket breaks down. Can you please tell what have i've done wrong?
This is the server side:
import java.net.*;
import java.io.*;
import java.net.InetAddress;
public class EchoServer {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = null;
Integer port = new Integer(args[0]);
try {
serverSocket = new ServerSocket(port);
}
catch (IOException e) {
System.err.println("Could not listen on port: "+port);
System.exit(1);
}
Socket clientSocket = null;
System.out.println ("Waiting for connection on port "+port+" ...");
try {
clientSocket = serverSocket.accept();
}
catch (IOException e) {
System.err.println("Accept failed.");
System.exit(1);
}
System.out.println ("Connection successful");
System.out.println ("Waiting for input.....");
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream(), "UTF-8"));
String inputLine;
while ((inputLine = in.readLine()) != null) {
System.out.println ("received: " + inputLine);
out.println(inputLine);
if (inputLine.equals("Bye."))
break;
}
out.close();
in.close();
clientSocket.close();
serverSocket.close();
}
}
and that's the client side
import java.io.*;
import java.net.*;
public class EchoClient {
public static void main(String[] args) throws IOException {
String serverHostname = new String (args[0]);
Integer port = new Integer(args[1]);
if (args.length > 0)
serverHostname = args[0];
System.out.println ("Attemping to connect to host " + serverHostname + " on port 10007.");
Socket echoSocket = null;
PrintWriter out = null;
BufferedReader in = null;
try {
echoSocket = new Socket(serverHostname, port);
out = new PrintWriter(echoSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(echoSocket.getInputStream(), "UTF-8"));
} catch (UnknownHostException e) {
System.err.println("Don't know about host: " + serverHostname);
System.exit(1);
} catch (IOException e) {
System.err.println("Couldn't get I/O for " + "the connection to: " + serverHostname);
System.exit(1);
}
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
String userInput;
System.out.print ("input: ");
while ((userInput = stdIn.readLine()) != null) {
out.println(userInput);
System.out.println("echo: " + in.readLine());
System.out.print ("input: ");
}
out.close();
in.close();
stdIn.close();
echoSocket.close();
}
}
It is not the ServerSocket that breaks.
The EchoServer reads from the client socket's InputStream in the loop while ((inputLine = in.readLine()) != null). When the socket connection is terminated, attempting to read will throw an exception. Since the loop isn't inside a try-catch-block, the exception kills the server process.
To prevent that, you need to handle the exception.
try {
while ((inputLine = in.readLine()) != null) {
System.out.println ("received: " + inputLine);
out.println(inputLine);
if (inputLine.equals("Bye."))
break;
}
} catch (IOException ex) {
System.out.println("Connection error... terminating.");
}

Java sockets send and receiving data

Hi I am trying to understand how Sockets work can implement a multiplayer side to a monopoly game.I understood how to create the connection, but now it seems I have trouble sending and receiving the data between the client and the server .Here is my code:
Client code:
public class EchoClient
{
public static void main(String[] args)
{
Socket echoSocket = null;
PrintWriter out = null;
BufferedReader in = null;
try
{
echoSocket = new Socket("127.0.0.1", 5000);;
out = new PrintWriter(echoSocket.getOutputStream());
in = new BufferedReader(
new InputStreamReader(echoSocket.getInputStream()));
BufferedReader stdIn = new BufferedReader(
new InputStreamReader(System.in));
String userInput;
while ((userInput = stdIn.readLine()) != null)
{
out.println(userInput);
}
out.close();
in.close();
stdIn.close();
echoSocket.close();
}
catch (UnknownHostException e)
{
System.err.println("Don't know about host: taranis");
}
catch (IOException e)
{
System.err.println("Couldent get I/O for "
+ " the connection to : taranis.");
}
}
}
Server code:
public class ServerSide
{
ServerSocket connect;
Socket connection;
PrintWriter out;
BufferedReader in;
public void go()
{
try
{
connect = new ServerSocket(5000);
connection = connect.accept();
in = new BufferedReader(
new InputStreamReader(connection.getInputStream()));
String userInput;
while ((userInput = in.readLine()) != null)
{
System.out.println("echo: " + in.readLine());
}
}
catch (IOException e)
{
System.out.println(e);
}
}
public static void main(String[] args)
{
new ServerSide().go();
}
}
I was trying to create here a simple connection between a client and a server.On the client side when the user inputs data I want it to be sent to the server and then print it it on the server console.It seems that the way I wright the code it isent working what did I do wrong?
First of all you need to declare a console object and then print on it.
Then don't forget to:
After every print on the console at the server side you need to flush the stream so the all the data will be printed.
Your code looks fine to me. Usually with sockets and keyboard input, you run into the case where the reader.readLine() hangs because it is still trying to read input from the other side. Typically, i will put an empty out.println() at the end of my client so the server will terminate the reading while loop. I've tried flush() before as Mike suggested but that seems to not work for me.
On the client, you're missing out.flush; after the out.println.
On the server,
while ((userInput = in.readLine()) != null) {
System.out.println("echo: " + userInput); // not in.readLine
}

Using Buffered reader and Socket

I wrote this simple Java program which connects to internic server and returns the domain details. However, I am facing a strange problem. I may sound dumb but here is the program!
import java.io.*;
import java.net.*;
public class SocketTest {
public static void main(String[] args) {
String hostName;
int i = 0;
try {
Socket socketClient = new Socket("whois.internic.net", 43);
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
InputStream in = socketClient.getInputStream();
OutputStream out = socketClient.getOutputStream();
System.out.println("Please Enter the Host Name!!");
hostName = bf.readLine();
hostName = hostName + "\n";
byte[] buf = hostName.getBytes();
out.write(buf);
while((i = in.read()) != -1) {
System.out.print((char)i);
}
socketClient.close();
} catch(UnknownHostException uht) {
System.out.println("Host Error");
} catch(IOException ioe) {
System.out.println("IO Error " + ioe);
} catch(Exception e) {
System.out.println("Exception " + e);
}
}
}
The program runs fine, without any runtime errors, but it shows no output when I try to print the result from internic server in the last piece of try block. I tried rearranging the code and found that if I place the bf.readLine() after creating socket streams, there is no output. However, if I place it before the socket creation (at the start of main method), the program displays intended output.
Is there any stream conflict or so? I am a newbie to networking in Java. The solution may be obvious but I am not able to understand! Please help me!!!
Move your input stream initialization after you send the domain to the output stream... This works for me locally:
import java.io.*;
import java.net.*;
public class SocketTest {
public static void main(String[] args) {
String hostName;
int i = 0;
try {
Socket socketClient = new Socket("whois.internic.net", 43);
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
OutputStream out = socketClient.getOutputStream();
System.out.println("Please Enter the Host Name!!");
hostName = bf.readLine();
hostName = hostName + "\n";
byte[] buf = hostName.getBytes();
out.write(buf);
InputStream in = socketClient.getInputStream();
while ((i = in.read()) != -1) {
System.out.print((char) i);
}
in.close();
out.close();
socketClient.close();
} catch (UnknownHostException uht) {
System.out.println("Host Error");
} catch (IOException ioe) {
System.out.println("IO Error " + ioe);
} catch (Exception e) {
System.out.println("Exception " + e);
}
}
}
Output:
Please Enter the Host Name!!
yahoo.com
Whois Server Version 2.0
Domain names in the .com and .net domains can now be registered
with many different competing registrars. Go to http://www.internic.net
for detailed information.
YAHOO.COM.ZZZZZZZ.GET.ONE.MILLION.DOLLARS.AT.WWW.UNIMUNDI.COM
YAHOO.COM.ZZZZZZ.MORE.INFO.AT.WWW.BEYONDWHOIS.COM
....Whole bunch more

InputStreamReader throws IOException

I'm trying to use an InputStreamReader to read bytes sent by a socket
I have
InputStreamReader in = new InputStreamReader(socket.getInputStream());
in.read();
This throws an IOException :(. I know the socket should be sending something but it keeps throwing the error.
The actual error message and stack trace would indeed be useful.
You could try using a BufferedReader and using readLine to make sure you are getting something.enter link description here
import java.io.*;
import java.net.*;
public class EchoClient {
public static void main(String[] args) throws IOException {
Socket echoSocket = null;
PrintWriter out = null;
BufferedReader in = null;
try {
echoSocket = new Socket("taranis", 7);
out = new PrintWriter(echoSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(
echoSocket.getInputStream()));
} catch (UnknownHostException e) {
System.err.println("Don't know about host: taranis.");
System.exit(1);
} catch (IOException e) {
System.err.println("Couldn't get I/O for "
+ "the connection to: taranis.");
System.exit(1);
}
BufferedReader stdIn = new BufferedReader(
new InputStreamReader(System.in));
String userInput;
while ((userInput = stdIn.readLine()) != null) {
out.println(userInput);
System.out.println("echo: " + in.readLine());
}
out.close();
in.close();
stdIn.close();
echoSocket.close();
}
}
Found here
That is expected behaviour, IOException is signalling that there is nothing to read, most probably socket has not connected properly. This question should be closed.

Categories