2016-06-16 4 views
-4

다른 사람과 마찬가지로 서버 소켓을 수락하여 해결할 수 없습니다. ? (잠금 영원히에, 나는 SS없이이 문제를 해결하려면 어떻게] 아래에 의견을알 수없는 호스트 예외 오류 [중복되지 않음]

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

를 참조 (또는 어떻게 든 소켓 잠금하는 문제를 해결

이 또한 실패에 대한 대안 시도 :

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

오류 메시지는 문제가 무엇인지 말합니다. 어떤 유형이 필요한지 * ([documentation] (https://docs.oracle.com/javase/8/docs/api/java/net/ServerSocket.html) 참조))? 어떤 유형이 제공됩니까? 제공된 -> 필요한 "논리"방법 있는가? – user2864740

+0

명백히'accept()'는 정적 메서드입니다. 따라서 클래스 자체가 아니라 ServerSocket의 인스턴스에서 메서드를 호출해야합니다. – RobotKarel314

답변

0

이 두 라인은 아무 의미도하지 않습니다;

당신이 실체화 새로운 ServerSocket의,하지만 당신은 .accept()를 호출하는 클래스를 사용합니다

ServerSocket ss = new ServerSocket(); 
ss = ServerSocket.accept(); 

쓰기이 대신 :

ServerSocket ss = new ServerSocket(); 
Socket socket = ss.accept(); 
+0

감사합니다. 이로 인해 주요 코드 문제가 해결되었습니다. 이제 코드를 실제로 만들어서 작업을 수행해야합니다 ... –

+0

좀 더 도와 줄 수 있습니까? 당신의 대답은 단지 더 큰 문제에 빠졌습니다. –

+0

질문을 완전히 변경했습니다 ... 알 수없는 호스트는 DNS가 호스트'pool-72-83-252-59.washdc.fios.verizon.net'을 확인할 수 없음을 의미합니다. ping을 시도하고 DNS 구성을 확인하십시오. –

0

후 귀하의 편집 :

ServerSocket가와 소켓은 여기에 완전히 무의미하다. 당신은 그들을 사용하지 않고 있습니다. 가능하면 코드가 accept()으로 차단됩니다. 둘 다 제거하십시오.

+0

그들은 어떻게 무의미한가요? 거기에 넣지 않으면 알 수없는 호스트 예외 오류가 발생합니다. [참조 : https://examples.javacodegeeks.com/core-java/net/unknownhostexception/java-net-unknownhostexception-how-to-solve-unknownhostexception/-- 해결하려면 "localhost"에 넣어야 함을 지정합니다. 이 오류] –

+0

당신이 그들을 사용하지 않기 때문에 그들은 당신의 편집 클라이언트 코드에서 무의미합니다, – EJP

관련 문제