Java - trouble running basic socket networking program - java

I'm trying to implement Sun's example Socket program, i.e. the KnockKnock server and client found here: http://download.oracle.com/javase/tutorial/networking/sockets/readingWriting.html
So I build the 3 files (EchoClient, KnockKnockServer, KnockKnockProtocol) into a project, build them, then go to cmd to run them:
> java KnockKnockServer
> Could not listen on port: 4444.
Also, I have trouble with the EchoClient (not that it means much since the server doesn't work). I get the following:
> java EchoClient
> Couldn't get I/O for the connection to: localhost
The one thing I changed in EchoClient class was to try and connect to "localhost" instead of their example machine "taranis". I don't understand the I/O error at all though.
So I need to figure this stuff out so I can later adapt it. Here's what I'm wondering: how do I know what port listen for in the KK Server? And if I want to connect to another computer in the EchoClient, would I directly put their (IPv4) IP address instead of "localhost"?
Thank you for any help

Try a different (higher port) because 4444 might already be in use on your machine:
Technical description for port 4444:
The port 4444 is specifically assigned to the Kerberos 5 authentication features particularly the implementation of Kerberos 4 in various systems including those running under the Mac OS X platform. The communication port 4444 is used in the conversion of Kerberos 5 credentials into an acceptable Kerberos 4 format.
source

That tutorial breaks rule #2 about handling exceptions: it makes up its own error message ' Couldn't get I/O for the connection to: ...' instead of printing the actual exception. Change it to do that, then you have some hope of finding out what went wrong.
I complained about that tutorial about eight years ago ;-(
(Rule #1 is print something.)

I had this problem yesterday when I was trying to learn the same thing you are!
1) Make sure both the server and client have the same port for example:
kkSocket = new Socket("localhost", 802); //Client
serverSocket = new ServerSocket(802); //Server
(I ran into this problem by accident)
2) Try changing both the server's port and the clients' port to 10000 or higher
3)The program outputs "Knock! Knock!" and than you need to type the input.(The hang you described might just be the server waiting for an input)

try this:
change taranishost name to localhost
kkSocket = new Socket("localhost", 4444);

Related

Why is my Android socketserver triggering a socket error 10061 in my client

I'n using Kotlin coroutines to setup a Java serversocket in Android studio 4.0 Beta 5. I'm running in the emulator on Windows 10. When my very reliable c language socket client attempts to connect using 127.0.0.1 as the IP it receives error 10061. The same client program has worked well for many years with a Java Swing socketserver.
Google give the following explanation for error 10061:
10061 is a Connection Refused error sent to you by the server. You could not make a
connection because the target machine actively refused it. The most common cause is a
misconfigured server, full server, or incorrect Port specified by the client.
Here's my code snippet
int myPort = 8080;
String localIP = InetAddress.getLocalHost().getHostAddress();
ServerSocket srv = new ServerSocket();
String hostname = InetAddress.getLocalHost().getHostName();
srv.bind(new InetSocketAddress(hostname,myPort));
srv.setSoTimeout(socketAcceptTimeOut); //This sets the timeout on the accept
Socket cli = srv.accept();
I used the server bind based on another stackoverflow answer but it did't help when I removed it. In any case I believe the serversocket is listening at 127.0.0.1. I'm using port 8080 but I've tried a few others.
On the Android side the srv.accept is just timing out.
What am I missing?
Thanks

Java ServerSocket binding to port Hamachi

So I've been working on a small application that would allow me to create a server, have the server connect/bind to a hamachi network/port, and allow the client to join the hamachi server, then use the client application to communicate and other such things over the server.
There is no good documentation that I've been able to find in the last 4 hours that specifies how to do this. I have a basic Server application that works fine on the "localhost", but I really want to make it work over hamachi.
code example:
InetAddress addr = InetAddress.getByName("**.***.***.***");
ServerSocket sSocket = new ServerSocket(****, 5, addr);
The error received is:
Error: java.net.BindException: Cannot assign requested address: JVM_Bind
I am using netbeans, and yes, I am 100% positive that the port is NOT in use. Please help!
Just specify null as the third argument to the constructor. That means 'listen at all local IP addresses'.
I suspect the IP address you're trying to bind to isn't local. You can't do that.

why Eclipse throws Exception when Server program runs while not in cmd.exe

I have written a simple code of server side programming that is waiting for the response from client, this runs successfully on the cmd.exe but while executing it on the eclipse it is throwing exception, the details of exception are as follows;
Exception in thread "main" java.net.BindException: Address already in
use: JVM_Bind
this is happening in eclipse only could anyone please tell me how it is working in eclipse & how in cmd.exe
the code of server side program is :
package networking;
import java.io.*;
import java.net.*;
public class Server1 {
public static void main(String args[]) throws Exception {
ServerSocket sock = new ServerSocket(5000);
Socket s1 = sock.accept();
BufferedReader br = new BufferedReader(new InputStreamReader(
s1.getInputStream()));
String msg = br.readLine();
}
}
The socket is open when 'Server1' is started.
This can happen if:
Server1 is already running. ( you didn't kill it, seems to be the likely case...)
Another process is listening on port 5000.
The port isn't closed down quite yet, but is stuck in a "waiting" state for a while. (It can happen if a server is killed when sockets are connected.)
But! Instead of guessing, You should simply check what's going on: Download Microsoft's TCPView and have a look:
http://technet.microsoft.com/en-us/sysinternals/bb897437
(You could also use netstat -a in a cmd window.)
Some notes:
The port number
You should probably not use port 5000, as it seems to be used by uPnP - you may find that the port is always open on some machines... Check here:
http://en.wikipedia.org/wiki/List_of_TCP_and_UDP_port_numbers.
Try a 5-digit number :-)
Time_wait
Check this question about Java network server and TIME_WAIT:
The should-be-accepted answer gives the following options to reduce the window,
setSoLinger(true,0)
setReuseAddress(true)
(With the risk of losing data when killing the server as the client may not become aware of that the send failed. In the old days, data could get mixed up too, but that is no longer the case.)
This used to happen to me all the time in eclipse. Make sure nothing else with respect to your server is running. In eclipse, make sure you click the red box to terminate all instances. To double check, if in windows, go to task manager and make sure no processes related to your server are running. That should do the trick.

JAVA vs Python Socket defaults

I'm pretty sure that my root problem is the antivirus app managed by our network, but my a basic socket client I wrote in Python that worked up until a short time ago is now giving me "socket error 10013". I also have the SocketTest app in JAVA. SocketTest works, but I have been unsuccessful in getting Python to connect.
The WIN 7 PC has two network cards, not bridged. I have switched the firewall on and off. If I disable the connection to the corporate LAN then the Python client is happy. Just to reiterate, this configuration worked for several months.
I cannot find the difference. Here is the sample code:
Python
MY_IP = '192.168.100.2'
MY_PORT = 62828
ROBOT_IP = "192.168.100.10"
ROBOT_SOCK = 29999
robotSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
robotSocket.bind((MY_IP, MY_PORT)) # Tried without binding
robotSocket.connect((ROBOT_IP, ROBOT_SOCK))
Result "socket error 10013"
JAVA:
socket = new Socket(ROBOT_IP, ROBOT_SOCK);
is = socket.getInputStream();
in = new BufferedInputStream(is);
while (!desonnected)
{
try
{
String got = readInputStream(in); // in.readLine();
This works fine, but I cannot find the difference to adjust my Python code. I'm sure it is the OfficeScan, but....
Edit
I ran WireShark as suggested. With both network cards enabled the Python connection request is not sent.
After disabling the corporate LAN, I compared the Connection requests between the JAVA client and the Python client and they contained the same data except for the local port of course.
I have recently struggled a lot with socket binding and multiple NICs on Windows 7.
I'm normally a unix guy, but I had to do some C#.Net for a friend as a favor..
MY problem was that I couldn't bind a socket to a specific port and ip on the client side. The solution was to increase the "binding priority" of the NIC I wanted to bind to. I don't know much about Windows, so I can't really elaborate more. Read this article for some info, it helped me: Network adapter card priority binding order in windows 7
Not sure if you're experiencing the same problem, as a socket error 10013 seems to be permission related. Google gives me this: dealing with socket error 10013

Socket.connect() to 0.0.0.0: Windows vs. Mac

Imagine the following code:
String hostName = "0.0.0.0";
int port = 10002;
int timeout = 5000;
Socket socket = new Socket();
socket.connect(new InetSocketAddress(hostName, port), timeout);
On the Mac it works fine and executes the connect (even with nothing running on port 10002) and on Windows I get the following exception:
java.net.SocketException: Permission denied: connect
What's the difference here and what would be the alternative on Windows? This is used in unit tests.
Regards
Jonas
Just in case somebody else stumbles upon this question, I am answering it.
Unfortunately, connecting to the any address is not allowed on Windows.
The Winsock function connect will return the error code WSAEADDRNOTAVAIL
[The remote address is not a valid address (such as INADDR_ANY or in6addr_any)],
as stated at the Windows API Documentation:
If the address member of the structure specified by the name parameter is filled with zeros, connect will return the error WSAEADDRNOTAVAIL.
So without using any localhost address, I think what you are trying to do will not be possible on Windows (Though I wonder if the Unix behavior is a bug or intentional.).
I would suggest setting up more loopback interfaces, as Mark Reed suggested in his comment.

Categories

Resources