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
Related
The goal of this practice program is simply to allow the client to continuously send strings of messages that the user inputs via typing and hitting enter key and prints it to console. The first time around , String messageFromClient = in.readLine(); in Server.java seems to block (Debugger won't let me step out). However after sending it a message like "hello", it continuously runs without blocking anymore.
I've been searching this up for a couple of hours but I guess I'm just not looking at the right places for the solution.
This link Java InputStream blocking read is probably the closest thing I've found that might help me.
Client.java
import java.io.*;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.*;
import java.net.InetAddress;
import java.util.Scanner;
public class Client {
static Socket connectionToServer;
public static void main(String[] args) {
try{
connectionToServer = new Socket("192.168.1.66", 6969);
InetAddress inetAddress = connectionToServer.getInetAddress();
System.out.print(
"Connection successful" +
"Connected to " + inetAddress.getHostAddress() +
"at " + inetAddress.getHostName() + "\n"
);
while(true) {
String input = System.console().readLine();
OutputStreamWriter dOut = new OutputStreamWriter(connectionToServer.getOutputStream());
dOut.write(input, 0, input.length());
//dOut.flush();
dOut.close();
}
} catch (IOException exception){
System.out.print("Failed to create socket");
}
}
}
Server.java
import java.io.*;
import java.net.*;
public class Server {
public static void main(String[] args) {
ServerSocket serverSocket;
try {
serverSocket = new ServerSocket(6969);
System.out.print("Server is up and running \n");
} catch (IOException exception){
serverSocket = null;
System.out.print("Cannot create ServerSocket");
}
while (true){
try {
System.out.print("Waiting from client.");
Socket socket = serverSocket.accept();
Thread newClientSocket = new Thread(new ServerThread(socket));
newClientSocket.start();
System.out.print("New thread created");
} catch (IOException exception){
System.out.print("Failed to create socket");
}
}
}
}
////////////////////////////////////////////////////////////
class ServerThread implements Runnable{
private Socket socket;
//constructor
ServerThread(Socket socket){
System.out.print("I am a new thread");
this.socket = socket;
}
public void run(){
while(true){
try{
System.out.print("Waiting for input.");
InputStreamReader inputStreamReader = new InputStreamReader(socket.getInputStream());
BufferedReader in = new BufferedReader(inputStreamReader);
String messageFromClient = in.readLine();
System.out.print(messageFromClient);
} catch (IOException exception) {
System.out.print(exception);
break;
}
}
}
}
You shouldn't construct a new BufferedReader for each loop iteration, since a BufferedReader will try to fill its buffer completely and possibly read beyond the end of the first line - and that data will be lost, since you're constructing a new BufferedReader after reading only the first line from it.
Move the construction of the BufferedReader out of the loop, that should help you. Also, make sure that you close the socket when you're done with it.
public void run() {
try {
try {
InputStreamReader inputStreamReader = new InputStreamReader(socket.getInputStream());
BufferedReader in = new BufferedReader(inputStreamReader);
while (true) {
System.out.print("Waiting for input.");
String messageFromClient = in.readLine();
System.out.print(messageFromClient);
}
} finally {
socket.close();
}
} catch (IOException exception) {
System.out.print(exception);
// TODO: handle exception
}
}
On the sending side, you shouldn't close the socket's OutputStream for every line, because once you close it, you can't use it anymore. And you should send a newline to the server after the string, as the Console.readLine() method doesn't include it in its return value.
OutputStreamWriter dOut = new OutputStreamWriter(connectionToServer.getOutputStream());
while(true) {
String input = System.console().readLine() + "\n";
dOut.write(input, 0, input.length());
}
dOut.close();
Console.readLine() does not include terminating characters. BufferedReader.readLine() blocks until it gets a terminating newline character
Change this:
String input = System.console().readLine();
To this:
String input = System.console().readLine() + System.lineSeparator();
I am trying to implement a client server socket connection where i am passing commands like ls/pwd over the GUI and I use an url(localhost) to establish the server connection at the port. Although i am able to establish a connection with client ,the code does not proceed beyond the Client Connection accepted state. I.e. it does not read the input at the server end which was sent by the client over the socket. Below are my three classes, Mainserver, ClientHandler(this handles the thread connections for the server)and the Client.
This is the Client Action button performed code:
private void jButton1ActionPerformed(java.awt.event.ActionEventevt) {
command = jTextField1.getText();
String url = jTextField3.getText();
try {
System.out.println("Before socket connection");
Socket socket = new Socket(url, 9002);
System.out.println("After socket connection");
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
System.out.println("After Buffered readers");
System.out.println("After getting streams");
if (socket != null) {
try {
int x = Integer.parseInt(command);
flag = 1;
} catch (Exception e) {
flag = 0;
}
if (flag == 0) {
String[] cmd = {"/bin/sh", "-c", command};
System.out.println("the value of command in GUI class is " + Arrays.toString(cmd));
try {
String commd = Arrays.toString(cmd);
System.out.println(commd);
out.write(commd);
input = in.readLine();
}
catch (IOException ex1)
{
Logger.getLogger(TestGUI.class.getName()).log(Level.SEVERE, null, ex1);
}
jTextField2.setText(input.toString());
}
}
}//try end of the first one
catch (IOException ex) {
Logger.getLogger(TestGUI.class.getName()).log(Level.SEVERE, null, ex);
}
The server class:
public class ServerMain {
public static void main(String[] args) throws IOException, InterruptedException {
int number, temp;
try {
ServerSocket serverSocket = new ServerSocket(9002);
System.out.println("server has been started in the server");
System.out.println("Server is waiting connection at" + InetAddress.getLocalHost().getCanonicalHostName() + "port" + serverSocket.getLocalPort());
while (true) {
Socket socket = serverSocket.accept();
System.out.println("Client Connection Accepted");
//pass on handling on this client to a thread
(new ClientHandler(socket)).start();
}
} catch (Exception e) {
System.err.println("Server already in use");
System.exit(-1);
}
}
}
The client Handler for the Server:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* #author ameerah
*/
public class ClientHandler extends Thread {
private static int BUFSIZE = 1024;
private StringBuffer result;
ServerSocket serverSocket;
String serverText;
StringBuffer output = new StringBuffer();
private Object serversocket;
public Socket getSock() {
return sock;
}
public void setSock(Socket sock) {
this.sock = sock;
}
Socket sock;
public ClientHandler(Socket sock) {
this.sock = sock;
}
#Override
public void run() {
PrintWriter outWriter = null;
try {
BufferedReader myInput = new BufferedReader(new InputStreamReader(sock.getInputStream()));
outWriter = new PrintWriter(sock.getOutputStream());
System.out.println(
"before accepting the command in server");
String inputLine;
while ((inputLine = myInput.readLine()) != null) //String command = myInput.readLine();
{
System.out.println(inputLine);
String result = "";
try {
result = executeCommand(inputLine);
} catch (IOException ex) {
Logger.getLogger(ClientHandler.class.getName()).log(Level.SEVERE, null, ex);
} catch (InterruptedException ex) {
Logger.getLogger(ClientHandler.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println(result);
outWriter.write(result);
}
} catch (IOException ex) {
Logger.getLogger(ClientHandler.class.getName()).log(Level.SEVERE, null, ex);
} finally {
outWriter.close();
}
}
public String executeCommand(String cmd)
throws IOException, InterruptedException {
try {
Process p = Runtime.getRuntime().exec(cmd);
p.waitFor();
BufferedReader reader
= new BufferedReader(new InputStreamReader(p.getInputStream()));
System.out.println("Inside the execute method");
String line = "";
while ((line = reader.readLine()) != null) {
output.append(line + "\n");
}
} catch (Exception e) {
e.printStackTrace();
}
return output.toString();
}
}
I have been at it for some time, and tried using different streams such as ObjectInputStream, ObjectOutputStream, but the code hangs each time. I cannot see at this point where Im going wrong :( I've searched in several forums but I do not still get an idea where Im going wrong here.Would appreciate any help.!
Best Regards
It was the readLine() which was expecting '\n' at the end. Therefore once i appended '\n' at the end and added out.flush() it was able to read and not keep hanging waiting for more inputs, and now the application is working.
Thank you very much for your helpful suggestions. The out.flush() advice proved to be very helpful.
Few tips to isolate the problem.
Check the value of command and catch Exception stack trace.
After out.write(commd); : add one more line out.flush(); After flush, server will get the data from client. Same is the case with outWriter. flush() should be called on outWriter after writing the data.
You are looking for an end of line to end your input loop but you are using write.
Change your send data statements to use println.
Client:
out.println(commd);
Server:
outWriter.println(result);
i am creating a simple client server communication . but i am unable to type anything in the command prompt.
code for server
i am unable to type anything in cmd when i run program
import java.net.*;
import java.io.*;
public class Tests
{
public static void main(String args[])
{
try
{
ServerSocket server = new ServerSocket(3000);
Socket c = server.accept();
DataOutputStream out = new DataOutputStream(c.getOutputStream());
DataInputStream in = new DataInputStream(c.getInputStream());
DataInputStream console = new DataInputStream(System.in);
String input,output;
while (true)
{
input = in.readLine();
System.out.println("Client says: " + input);
output = console.readLine();
out.writeBytes(output+ '\n');
}
}
catch(IOException e)
{ System.out.println("IO Error in streams " + e); }
}
}
code for client
please help me
import java.io.*;
import java.net.*;
public class Testc
{
public static void main(String[] args)
{
try
{
Socket client = new Socket("127.0.0.1", 3000);
DataOutputStream out = new DataOutputStream(client.getOutputStream());
DataInputStream in = new DataInputStream(client.getInputStream());
DataInputStream console = new DataInputStream(System.in);
System.out.println("Connected. Enter text:");
String input,output;
while (true)
{
input = in.readLine();
System.out.println("server says : "+ input);
output = console.readLine();
out.writeBytes(output + '\n');
}
}
catch (UnknownHostException e)
{ System.err.println("error"); }
catch (IOException e)
{ System.err.println("I/O error"); }
}
}
I'm creating a server where clients connect to play a guessing game they are also to get points from doing this.
My only problem at the moment is that whenever my client guesses the number correctly it jumps to the server and says 'server null'. I want the guessing game to continue until the client inputs 'goodbye' - on which his/her score is given.
Here is my code could you point out where I've gone wrong and advise me on how I would achieve what I want. I think the problem is in protocol I probably just need to put the while in the correct place, so that's up first. Thanks folks!
Just to add, the variables are named oddly I'm aware of this it was previously a knock knock joke server
Protocol
import java.util.*;
public class KKProtocol {
int guess = 0, number = new Random().nextInt(100) + 1;
int score = 0;
Scanner scan = new Scanner(System.in);
public String processInput(String theInput) {
String theOutput = null;
System.out.println("Please guess the number between 1 and 100.");
while (guess != number) {
try {
if ((guess = Integer.parseInt(scan.nextLine())) != number) {
System.out.println(guess < number ? "Higher..." : "Lower...");
}
else {
System.out.println("Correct!");
score = 1;
}
}
catch (NumberFormatException e) {
System.out.println("Please enter a valid number! If you want to Quit just say'Goodbye'");
}
}
return theOutput;
}}
Server
import java.net.*;
import java.io.*;
public class KKServer {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = null;
boolean listening = true;
try {
serverSocket = new ServerSocket(4040);
} catch (IOException e) {
System.err.println("Could not listen on port: 4040.");
System.exit(-1);
}
System.err.println("Started KK server listening on port 4040");
while (listening)
new KKThread(serverSocket.accept()).start();
System.out.println("Accepted connection from client");
serverSocket.close();
}
}
Thread
import java.net.*;
import java.io.*;
import java.util.Scanner;
public class KKThread extends Thread {
private Socket mySocket = null;
public KKThread(Socket inSocket) { //super("KKThread");
mySocket = inSocket;
}
public void run() {
try {
PrintWriter out = new PrintWriter(mySocket.getOutputStream(), true);
Scanner in = new Scanner(mySocket.getInputStream());
String inputLine, outputLine;
KKProtocol kkp = new KKProtocol();
outputLine = kkp.processInput(null); // first time only
out.println(outputLine); // (Should be "Knock Knock")
while (true) {
inputLine = in.nextLine(); // read in client input
outputLine = kkp.processInput(inputLine); // get reply from protocol
out.println(outputLine); // send it out to socket
if (outputLine.equals("Bye"))
break;
}
out.close();
in.close();
mySocket.close();
} catch (Exception e) {
System.err.println("Connection reset"); //e.printStackTrace();
}
}
}
Client
import java.io.*;
import java.net.*;
import java.util.Scanner;
public class KKClient {
public static void main(String[] args) throws IOException {
Socket kkSocket = null;
PrintWriter out = null;
Scanner in = null;
try {
kkSocket = new Socket("127.0.0.1", 4040);
out = new PrintWriter(kkSocket.getOutputStream(), true);
in = new Scanner(new InputStreamReader(kkSocket.getInputStream()));
} catch (UnknownHostException e) {
System.err.println("Don't know about host.");
System.exit(1);
} catch (IOException e) {
System.err.println("Couldn't get I/O for the connection");
System.exit(1);
}
Scanner stdIn = new Scanner(System.in);
String fromServer = in.nextLine();
while (true) {
System.out.println("Server: " + fromServer);
if (fromServer.equals("Bye."))
break;
out.println(stdIn.nextLine());
fromServer = in.nextLine();
}
out.close();
in.close();
stdIn.close();
kkSocket.close();
}
}
'
Why does it jump to the server and says 'server null'? How do I continue the guessing until the client inputs 'goodbye'?
You are currently not assigning the server response for the score in KKProtocol.processInput() so a null is returned instead resulting in the message that you see:
Server: null
You could use:
theOutput = Integer.toString(score);
Also your score is fixed at 1 so you may wish to devise a scoring system perhaps based on the number of guesses used.
In you processInput()method, you are not returning any value, but allways null.
It looks like that null value is transformed into the String "null" on output and sent to the client.
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.