Sending TCP/IP request from website using local port - java

I want to create a web utility which will use a local port for sending a TCP/IP request.
Does it possible to use a client side port to send a TCP/IP request?
I know it is possible if we send a TCP/IP request from a web server and the specific port is allowed on server. But I want to send the request using the client side port.
What would I need to do? Should I create a Java Applet/Plugin or is it possible using PHP/.net?

It depends what you want to do, but fundamentally you can't initiate operations on the client using server side code like PHP.
Javascript is capable of some networking operations using XHR (Ajax) and WebSockets, and wrapping libraries such as socket.io. Perhaps that will suffice for your needs.
A Java Applet will be more capable, but will be restricted in what it can do unless you sign the code. And of course will be slower to start up for the client.

Related

Fetching Client IP's Client Sided

Is it possible to fetch the IP addresses of connected clients to a server from the client side? I know it's possible server sided, but is it client sided?
Note: I'm talking about server-client connection using a basic Socket.
Only if the server purposely provides that list.
Otherwise, it is not possible to get any information of other clients connected to a server you are connected to (this, of course, applies to Java, but can also be understood as a general concept of networking -- in the context of peer to peer, client/server, sockets).
Not without a script or program on the client end of the socket having code to retrieve it (e.g. Javascript on a web page). You cannot tell this strictly from the server side.
You are talking with your server, and only server talks with other clients directly. So only server can send you clients ip addresses.
So, this is possible, but must be implemented on server.

Netty4 application as both client and a server

I want to write a program in Netty4 that should act as a server to other clients and also it itself is a client to another server. How to do this in Netty4? So far all examples I have seen are either client or server. Thanks.
There are no special difficulties here. You need to create a part that will act as a server (using ServerBootstrap), and a part that will act as a client (using Bootstrap).
If you need to establish a connection to another server while handling incoming connection from a client, you can place that logic into a ChannelHandler of the server's pipeline.
Netty provides two examples of this approach:
Hex dumping proxy
SOCKS proxy

is posible different language on web socket?

i have the problem when i want to try web socket technology on my application.
but i want to ask about requiretment of web socket.
1) is posible i'm using Server with javascript language ex is socket.io
var io = require('socket.io').listen(3001);
io.sockets.on('connection', function (socket) {
socket.emit('news', {hello: 'can you hear me'});
});
and the client using Java language example is socket.io.java
i'm using this method to get message from server
public void onMessage(JSONObject json, IOAcknowledge ack) {
Log.d("We received a message: " , json.toString());
}
with my socket server is
SocketIO socket = new SocketIO("http://127.0.0.1:3001/news");
2) is support to all machine? or browser?i'm newbie on web socket programing, please help me
thanks for your answer...
Yes, you can build the server in any language you want, but web sockets are not a simple socket, they are sockets that "follow" a specific protocol, so make sure you read the specification of this protocol and implement it on your server so any browser will be able to stablish a connection to it.
And you can also write a client in any language, but if you're not using javascript then I don't see why you would use web sockets instead of using just a simple socket. But if you want to, just take a look at what data a browser sends to a web socket server and emulate it from your own client.
To elaborate on Delta's answer, "websockets" is a variation of the HTTP protocol where the client sends an HTTP request message, the server sends an HTTP response, and then the client and server use the still-open TCP/IP connection to do "other things". The request and response contain special headers which allow the client and server to agree to use the connection in this way.
In order for this to work, the client and server both need to understand at least a subset of the HTTP protocol in order to do the initial "handshake". Hence you can't simply use a websocket client to talk to a plain socket server ... or vice versa. (If you try to do that, both ends will see unexpected stuff / protocol errors. And the websocket end should promptly close its end of the TCP/IP connection.)
Having said that, a websocket client and a websocket server can be implemented in just about any modern programming language. (And the same goes for plain socket clients and servers.)
Sockets are an operating system thing. You can use them with any language providing the relevant interface or glue code to the operating system calls implementing them (e.g. on Linux: socket(2), connect(2), accept(2), listen(2), poll(2), recv(2) etc...)
But if you are newbie about sockets, I strongly suggest reading a good network programming book.

Can you connect an HTML5 web socket to a Java Socket?

I had set up a system that had a Java program running on a server and a Java applet embedded in a page on a client's browser and the two communicating via Java sockets. I'm wondering if I can switch over from a Java applet to just HTML5 and javascript, using a WebSocket on the client side for communication with the Java socket on the server.
Is there a simple way to make a WebSocket communicate with a Java Socket?
Is there a simple way to make a WebSocket communicate with a Java Socket?
From what I understand, WebSocket works by the client side opening a port 80 connect to the server side, and sending a variant HTTP 1.1 request to the server to negotiate a WebSocket connection. If the server recognizes this, it will send a suitable response, and then allow the still open TCP connection to be used for full-duplex client-server interactions.
It looks like it would be possible to quickly put together a server-side that just understood WebSocket negotation and not full HTTP. However, I think you are better off looking at existing WebSocket implementations, including those embedded in HTTP servers / protocol stacks.
This Wikipedia page compares a number of WebSocket implementations, and should help you in deciding which server-side implementation to use.
But to directly answer your literal question, a WebSocket client can only connect to a WebSocket-aware server; i.e. that one that can perform the initial negotiation. (On the client side, you could implement starting from a bare Socket, but you would need to implement all of the "HTTP stuff" on top of that ... for the setup phase.)
Nope, you cannot communicate using regular sockets with client WebSockets.
WebSockets are special HTTP requests, with an upgrade in the HTTP Header, and a standard protocol to establish a connection (see the official RFC doc).

how do i send data from a webpage to port in web server?

which is the best method to send a numerical value data from a javscript webpage to a web server port.. i have a server socket program running on the server and listening to port 5000.
i need to send data from a webpage to the port? which is the best method?
the scene is this.
i have a C socket program running at a web server hosted in my laptop using XAMPP.
it is listeing to a port 5000 and also i have my ip address. in the web page,
i am running a javascript and when the user presses any of the arrow keys i need a value to be sent to this port.. like up -1 , down -2 etc ...
There isn't currently any way to do this in a portable fashion. Some browsers support the WebSocket standard, which gives you some limited access to sockets, but this isn't currently available in any common browsers other than Chrome and Safari. The Flash player supports a separate XMLSocket protocol, which you can also use if the Flash plugin is available. However, neither of these protocols supports raw access to sockets -- both require some initial negotiation and packet framing.
Because your web server is running on a different port than your socket program, the Javascript in the browser is restricted from making requests directly to the socket program because of the Same Origin Policy. However, there are various ways to get around this restriction. Alex Sexton has a lengthy overview of all the different ways of doing this. However, all of those ways require that your socket program be running the http protocol also.
Socket.IO seems like a perfect fit for what you are trying to do, but you'll probably need to ditch your C program.
The only way to open a network socket connection from a web page is to use a Java or Flash app to make the binary socket connection. You could then use java script to pass the data to the app which would connect and forward it to the server.
Alternatively you could use some server side code and AJAX to proxy the connection and forward the data.
The second option doesn’t rely on any browser plug-in being installed and I think would be easier to implement. It would also be able to forward to a port that is not on the web server itself as it wouldn’t be subject to browser security restrictions.
I guess making one ajax request to required port on a server... can solve the request.

Categories