I can't get the client script to connect to the localhost server, the TCP connection never happens - swift

I wrote a simple client side program that creates a socket using
CFSteamCreatePairWithSocketToHost function
and connects to the server that runs on the local host on port 8080. It creates the socket just fine but it never connects to the server. I wrote the server in C. It didn't work and gave me a
kCFErrorDomainCFNetwork error 72000
and the only information that relays is that apparently the TCP connection couldn't be made don't know why though. So I tried to write the client side script in C too and added it to my Swift project bridging header and all but it still doesn't connect. It creates the socket just fine but it fails to connect to the server and I have no idea why.
But the same C client script worked when I compiled it using clang and ran it but didn't connect when I ran it with my swift project in Xcode. Is my mac blocking the libraries from making a TCP connection or something?
I don't even know what to search for. The only thing I found was an issue on a Github library called starscream which had the same errors I had and I'm not even using that library and the reply there was "the only thing we can discern from this error is that the TCP connection was unsuccessful".
Here's the code I used to connect to the server using Swift 4. The server is running on port 8080 on localhost.
class client:NSObject {
var inputstream = InputStream!
var outputstream = OutputStream!
func setupNetworkCom() {
var readstream = Unmanaged<CFReadStream>?
var writestream = Unmanaged<CFWriteStream>?
CFStreamCreatePairWithSocketToHost(kCFAllocatorDefault, "localhost" as CFString, 8080, &readstream, &writestream)
inputstream = readstream!.takeRetainedValue()
outputstream = writestream!.takeReatainedValue()
inputstream.schedule(in: .current, forMode: .common)
outputstream.schedule(in: .current, forMode: .common)
inputstream.open()
outputstream.open()
}
}
I've also tried replacing "localhost" with "127.0.0.1" which is the IP I specified for the server to run on but it still doesn't work.

click on your project settings and go to capabilities there you'll see the app sandbox. make sure it's turned on and then enable incoming connections and outgoing connections.

Related

LibGDX: Error making a socket connection to *ip-adress*

I want to make 2 devices communicate via sockets.
I use this code for the client socket:
Socket socket = Gdx.net.newClientSocket(Net.Protocol.TCP, adress, 1337, socketHints);
(SocketHints: timeout = 4000)
I get a GdxRuntimeException each time this line is being executed. What is wrong with the socket?
Screenshot of stack trace
You get that message because the socket couldn't be opened.
Note the last line about the return in the API:
newClientSocket:
Socket newClientSocket(Net.Protocol protocol,
java.lang.String host,
int port,
SocketHints hints)
Creates a new TCP client socket that connects to the given host and port.
Parameters:
host - the host address
port - the port
hints - additional SocketHints used to create the socket. Input null to use the default setting provided by the system.
Returns:
GdxRuntimeException in case the socket couldn't be opened
Try doing some debugging to find out why you are getting this error.
Is the port already in use? Are you trying to open more than one connection on the same port? Is the server IP valid? Maybe something else is causing the issue?

How can I debug the following Go code, which tries to make a TCP connection to an IP address and port?

I am getting an IP address and port number from a Bittorrent tracker, for a specific torrent file. It represents a peer on the bittorrent network. I am trying to connect to the peer using this code. The connection always times out (getsockopt: operation timed out). I think I am missing something very fundamental here, because I tried the same code in python with the exact same result, operation timed out. It happens for every single peer IP address.
I downloaded this bittorrent client - https://github.com/jtakkala/tulva
which is able to connect to peers from my system using this type of code (Line 245, peer.go). I have also been able to use similar code for connecting to a tcp server running on localhost.
Edited details after JimB's comment and Kenny Grant's answer
package main
import (
"fmt"
"net"
)
func main() {
raddr := net.TCPAddr{IP: []byte{}/*This byte slice contains the IP*/, Port: int(/*Port number here*/)}
conn, err := net.DialTCP("tcp4", nil, &raddr)
if err != nil {
fmt.Println("Error while connecting", err)
return
}
fmt.Println("Connected to ", raddr, conn)
}
Try it with a known good address, and you'll see your code works fine (with a 4 byte IPv4 address for SO say). Bittorrent peers are transient, so it probably just went away, if testing you should use your own IPs that you know are stable.
raddr := net.TCPAddr{IP: net.IPv4(151, 101, 1, 69), Port: int(80)}
...
-> Connected to {151.101.1.69 80 }
if you're trying to connect to 187.41.59.238:10442, as jimb says, it's not available. For IPs, see the docs:
https://sourcegraph.com/github.com/golang/go#9fd359a29a8cc55ed665542d2a3fe9fef8baaa7d/-/blob/src/net/ip.go#L32:6-32:8

Setup a datastreaming server in processing

I want to setup a datastreaming server in Processing, so the Client sends a String to the Server and the Server answeres it. For example Client - Server "Cupcake" then Server - Client "Cupcakce sounds funny" so the Server answeres the string. I tried this with the UDP library and opened the port on the server. But when the server had to answer the Clinet it did'nt work, because I can't open the client's ports. Any solutions?
Sounds like you need two-way communication.
Using UDP you would need two sketches that are both UDP servers and clients.
e.g.
sketch #1 listens on port 12000
sketch #1 sends data on port 12001
sketch #2 listens on port 12001
sketch #2 sends data on port 12000
You can also use TCP sockets.
As the Server you can use Examples > Libraries > Network > ChatServer
I'm surprised there's no ChatClient example, but you can get away with something like this:
import javax.swing.*;
import processing.net.*;
int port = 10002;
Client myClient;
void setup()
{
size(400, 400);
textFont(createFont("SanSerif", 16));
myClient = new Client(this, "localhost", port); // Starts a client on port 10002
background(0);
}
void draw()
{
background(0);
text("client - press ENTER to type\nconnected:"+myClient.active(), 15, 45);
}
void keyReleased() {
if (keyCode == ENTER) {
String message = JOptionPane.showInputDialog(null, "message: ", "TCP Client messaging", JOptionPane.QUESTION_MESSAGE);
println(message);
if (myClient.active() && message != null) {
myClient.write(message);
}
}
}
Note: The server must be running before the client so the client can connect.
Be sure to checkout the difference between UDP and TCP protocols to work out which one makes most sense to use in your case (especially if you pan to use more clients).
Another option worth looking into is WebSockets. This would allow you to have a WebSocket server in Processing and the client could either be another Processing sketch or simply any browser with WebSocket support(e.g. most modern)

How can we get port 8080 from adempiere so as to give it dynamically

I have a message sending program from server to client which is working inside adempiere. Here I have to give supply port: 8080 dynamically, ie. port must not be hardcoded. Now I am hard coding port 8080 at serversocket and socket
Server
ServerSocket srvr = new ServerSocket(8080, 1, InetAddress.getByName(mSession.getRemote_Addr()));
Client
Socket skt = new Socket(ip.getHostAddress(), 8080);
Please suggest a method rebel to this hard coding.
Please help me.
The web port is part of the configuration data that is used when the setup process is run but it isn't accessed by the server/client once the setup is complete. To access the data, you will need to load the configuration data again like this:
int webPort = 8080;
ConfigurationData data = new ConfigurationData(null);
if (data.load()) {
webPort = data.getAppsServerWebPort ();
}
ServerSocket srvr = new ServerSocket(webPort, 1, InetAddress.getByName(mSession.getRemote_Addr()));

Can I set up socket.io chat on heroku?

I have a simple socket.io chat application which I've uploaded to one of the new Heroku 'cedar' stacks.
Now I almost have everything working but I've hit one stumbling block. On my localhost, I open a connection to the socket server from the client with:
// lots of HTML omitted
socket = new io.Socket('localhost', {port: 8888});
But on Heroku, I obviously must substitute something else in for these values.
I can get the port from the process object on the server like so:
port = process.env.PORT || 8888
and pass that to the view.
But what do I substitute for 'localhost'?
The correct way according the article on heroku is:
io.configure(function () {
io.set("transports", ["xhr-polling"]);
io.set("polling duration", 10);
});
socket = new io.Socket();
This ensures that io.Socket won't try to use WebSockets.
I was able to get Socket.IO v0.8 to work on Heroku Cedar by doing the following:
Within the Express app (in CoffeeScript in my case):
app = express.createServer();
socket = require("socket.io")
...
io = socket.listen(app);
io.configure () ->
io.set("transports", ["xhr-polling"])
io.set("polling duration", 10)
io.sockets.on('connection', (socket) ->
socket.on('myaction', (data) ->
...
socket.emit('result', {myData: data})
### The port setting is needed by Heroku or your app won't start
port = process.env.PORT || 3000;
app.listen(port);
And within the front-facing Javascript of your application:
var socket = io.connect(window.location.hostname);
function sendSocketRequest() {
socket.emit('myaction', $("#some_field").val());
}
socket.on('result', function(data) {
console.log(data);
}
Helpful links:
Heroku Node help
Heroku Socket.IO help
This has now changed as of Oct 2013, heroku have added websocket support:
https://devcenter.heroku.com/articles/node-websockets
Use:
heroku labs:enable websockets
To enable websockets and dont forget to remove:
io.configure(function () {
io.set("transports", ["xhr-polling"]);
io.set("polling duration", 10);
});
After trying every combination under the sun I finally just left it blank. Lo and behold that works perfectly. You don't even need the port.
socket = new io.Socket();
I was also having this problem on heroku. I was able to make it work using the hostname "myapp.herokuapp.com" (or simply window.location.hostname, to work both local and in production) and setting the port to 80. I'm using SocketIO 0.6.0.
Wouldn't you just put your actual hostname?
2011-06-25T21:41:31+00:00 heroku[router]: Error H13 (Connection closed without response) -> GET appxxxx.herokuapp.com/socket.io/1/websocket/4fd434d5caad5028b1af690599f4ca8e dyno=web.1 queue= wait= service= status=503 bytes=
Does this maybe mean the heroku router infront of the app is not configured to handle web socket traffic?
[update]
It would appear as of 6/22/2011 the answer is yes... heroku does not support socket.io see this post: http://blog.heroku.com/archives/2011/6/22/the_new_heroku_2_node_js_new_http_routing_capabilities/