无法通过像其他人那样接受服务器套接字来解决此问题。 (永远锁定;见下面的评论]
import java.net.*;
import java.io.*;
public class one_hundred
{
public static void main(String [] args)
{
String hostName = "pool-72-83-252-59.washdc.fios.verizon.net";
int portNumber = 3281;
try
{
/*
Open a socket.
Open an input stream and output stream to the socket.
Read from and write to the stream according to the server's protocol.
Close the streams.
Close the socket.
*/
Socket server = new Socket(hostName, portNumber);
OutputStream outToServer = server.getOutputStream();
DataOutputStream out = new DataOutputStream(outToServer);
out.writeUTF("mJFr1vJBIcpYTtIui6yLrzQw " + server.getLocalSocketAddress());
InputStream inFromServer = server.getInputStream();
DataInputStream in = new DataInputStream(inFromServer);
System.out.println(in.readUTF());
server.close();
}catch(IOException e)
{
e.printStackTrace();
}
}
}
如何在没有ss的情况下解决这个问题? (或者以某种方式解决套接字锁定问题?
另一种尝试也失败了:
import java.io.*;
import java.net.*;
import java.net.Socket;
public class onehundred
{
public static void main(String args[]) throws java.io.IOException
{String urlParameters = "fName=" + URLEncoder.encode("???", "UTF-8") + "&lName=" + URLEncoder.encode("???", "UTF-8");
request("http://codeabbey.sourceforge.net", urlParameters);}
public static String request(String gotourl, String urlParameters)
{ HttpURLConnection connection = null;
try
{ InetAddress locIP = InetAddress.getByName("0.0.0.0");
ServerSocket serverSocket = new ServerSocket(8080, 0, locIP);
//Create connection
URL url = new URL(gotourl);
connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("/say-100.php", "Token: 9PaF2QQmZBFX+K3+LlI5ozky");
connection.setUseCaches(false);
connection.setDoOutput(true);
//Send request
DataOutputStream wr = new DataOutputStream (connection.getOutputStream());
wr.writeBytes(urlParameters);
wr.close();
//Get Response
InputStream is = connection.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
StringBuilder response = new StringBuilder();
String line;
while((line = rd.readLine()) != null)
{ response.append(line);
response.append('\r');
}
rd.close();
System.out.println(response); //System.out.println(line); also prints out nothing
return response.toString();
} catch (Exception e)
{e.printStackTrace();
return null;
} finally
{
if(connection != null)
{connection.disconnect();}
}
}
}
答案 0 :(得分:0)
这两行毫无意义;
您实例化一个新的ServerSocket,但您使用Class来调用.accept();
ServerSocket ss = new ServerSocket();
ss = ServerSocket.accept();
改为写下:
ServerSocket ss = new ServerSocket();
Socket socket = ss.accept();
答案 1 :(得分:0)
发布您的编辑:
ServerSocket和Socket在这里完全没有意义。你没有使用它们。您的代码将在accept()
中阻止,可能会永久阻止。将它们都移除。