JAVA vs Python Socket defaults - java

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

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

Connect to Matrikon server with OPC UA Java Legacy

I am trying to connect to an OPC server of a Siemens S7 1200 PLC. For this I used the Matrikon application
That is configured on my local machine. With the JeasyOPC library I can make the connection, so it is like this:
JOpc jopc = new JOpc("localhost","Matrikon.OPC.SiemensPLC.1","JOPC1");
JOpc.coInitialize();
But this library can only be used in Windows and has problems when compiling binaries in 64.
So I have tried with OPC Foundation UA JAVA Legacy and I would like to make the same connection, however in the examples we ask for many more things:
String publicHostname = InetAddress.getLocalHost().getHostName();
String url = "opc.tcp://localhost:102/"; // ServerExample1
// String url = "Matrikon.OPC.SiemensPLC.1"; // This not work for me
EndpointDescription[] endpoints = myClient.discoverEndpoints(url);
I would like to emulate the operation of jeasyOPC as far as possible, in any case I can not find any example that works for me.
I would appreciate any example that would help me have a base client that would work with the Matrikon server
Thank you.
Okay. I answer to myself.
After many searches I have found my error:
There are two types of OPC: DA and UA.
The first of all (the DA) is to which I could connect with:
JOpc jopc = new JOpc ("localhost", "Matrikon.OPC.SiemensPLC.1", "JOPC1");
This version of the protocol is the one used historicaménte windows, uses COM libraries and can only be implemented under a Windows computer.
The second, the OPC UA, is the new implementation and connects like this:
String url = "opc.tcp: // localhost: 102 /"; // ServerExample1
This version is already compatible with Windows, Linux and any system that is capable of running the libria.
The problem was that, in my case, the S7 1200 only uses OPC DA, which made the UA option unfeasible for me if it did not use a compatible gateway.
I hope this information will help someone who is in my situation.
Greetings.

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.

Java - trouble running basic socket networking program

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);

RMI calls not working when firewall present on the WindowsXP client, but fine on Windows 7 client

I am using the basic RMI-based client/server app shown in Java Tutorials. I am running the server and client on different machines. I also export the remote objects for both the server and client, and make them available to each other.
However, there is a firewall turned on the client side and so the RMI calls fail (from client to server or server back to client). Also, when the machine has Windows 7 instead of WindowsXP, just allowing the popup message from firewall makes everything run fine. Somehow, the same does not happen on WindowsXP.
I have looked at http://download.oracle.com/javase/6/docs/technotes/guides/rmi/faq.html#firewall mentioned in other similar questions and I am trying the fixed port approach (where in remote object listens on a fixed port).
The code is something like this:
Server
Compute engine = new ComputeEngine();
Compute stub = (Compute) UnicastRemoteObject.exportObject(engine,1299);
Registry registry = LocateRegistry.createRegistry(1299);
registry.rebind("Compute", stub);
Client
String host = "192.168.x.y";
Registry registry = LocateRegistry.getRegistry(host, 1299);
Compute comp = (Compute) registry.lookup("Compute");
Pi task = new Pi(Integer.parseInt(args[0]));
BigDecimal pi = comp.executeTask(task);
Please let me know if you have faced similar problem or if you can point out where I am going wrong.
Thanks,
Abhinav.
first try comment out this line:
Registry registry = LocateRegistry.createRegistry(1299);
and check do you have security.policy file if not then create that
then try this code in that:
grant {
permission java.security.AllPermission "", "";
};
and might be you are not setting security manager:
set that like this in your main method:
System.setSecurityManager(new RMISecurityManager());

Categories

Resources