W / System.err:java.net.ConnectException:连接超时

时间:2018-09-01 00:36:41

标签: java android sockets heroku

我正在尝试连接到Heroku上的Node服务器应用程序:

Request

但是,它总是超时:

public async Task<HttpResponseMessage> PostFormData()
{
    // Check if the request contains multipart/form-data.
    if( !this.Request.Content.IsMimeMultipartContent() )
    {
        throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
    }

    // Temporarily write the request to disk (if you use `MultipartMemoryStreamProvider` your risk crashing your server if a malicious user uploads a 2GB+ sized request)
    String root = this.Server.MapPath("~/App_Data");
    MultipartStreamProvider provider = new MultipartFormDataStreamProvider(root);

    try
    {
        // Read the form data and serialize it to disk for reading immediately afterwards:
        await this.Request.Content.ReadAsMultipartAsync( provider );

        // This illustrates how to get the names each part, but remember these are not necessarily files: they could be form fields, JSON blobs, etc
        foreach( MultipartFileData file in provider.FileData )
        {
            Trace.WriteLine( file.Headers.ContentDisposition.FileName );
            Trace.WriteLine( "Server file path: " + file.LocalFileName );
        }

        return this.Request.CreateResponse( HttpStatusCode.OK );
    }
    catch( System.Exception e )
    {
        return this.Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e);
    }
}

将目标服务器更改为Google.com也无济于事。

Android

const express = require('express')
const path = require('path')
const PORT = process.env.PORT || 5000

var app = express();

app.get('/', function (req, res) {
   res.send('Hello World');
})

var server = app.listen(PORT, function () {
   var host = server.address().address
   var port = server.address().port

   console.log("Example app listening at http://%s:%s", host, port)
})  

为什么连接不成功?

我已经设置了Internet选项:

    SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
09-01 03:24:43.813 24507-25053/myapp.ca.my_init_application W/System.err: java.net.ConnectException: Connection timed out

谢谢大家。

1 个答案:

答案 0 :(得分:2)

套接字尚未绑定,因此,如果您尝试使用此new ServerSocket(0).getLocalPort();,它将返回-1。您需要保留端口号的对称实习生,所以使用

Socket clientSocket = new Socket(serverName,5000);