2012-12-04 124 views
1

我一直有一個與服務器套接字連接的問題,我有一個這個小遊戲的程序,並試圖使用套接字來獲得它連接,但是當我到達serverSocket = new ServerSocket(53000);它只是停止和凍結,doesn此後似乎沒有任何關係,或者採取任何聯繫。並且在kkSocket = new Socket(duke, 53000);之後,它剛好崩潰,似乎沒有連接,我確定我的防火牆沒有打開,我添加了互聯網權限,並且我嘗試了許多不同的設備名稱,我已將什麼我有我的設備命名,嘗試當前的IP地址,並沒有什麼會通過,我有一個銀河聯繫和nexus 7平板電腦,他們都有問題時,試圖溝通彼此,任何信息將是偉大的,謝謝。ServerSocket連接問題

package com.idlethought.handagedon; 

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.io.PrintWriter; 
import java.net.InetAddress; 
import java.net.ServerSocket; 
import java.net.Socket; 
import java.net.UnknownHostException; 

import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentActivity; 
import android.view.DragEvent; 
import android.view.Gravity; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.View.OnDragListener; 
import android.view.ViewGroup; 
import android.view.View.OnClickListener; 
import android.view.ViewGroup.LayoutParams; 
import android.widget.Button; 
import android.widget.ImageView; 
import android.widget.LinearLayout; 
import android.widget.PopupWindow; 
import android.widget.Toast; 

public class ImageSelectionFragment extends Fragment { 
    private static final String IMAGE_DATA_EXTRA = "resId"; 
    private int mImageNum; 
    private ImageView mImageView; 
    private String myHand = null; 
    private String theirHand = null; 
    private ServerSocket serverSocket; 

    static ImageSelectionFragment newInstance(int imageNum) { 
     final ImageSelectionFragment f = new ImageSelectionFragment(); 
     final Bundle args = new Bundle(); 
     args.putInt(IMAGE_DATA_EXTRA, imageNum); 
     f.setArguments(args); 
     return f; 
    } 

    // Empty constructor, required as per Fragment docs 
    public ImageSelectionFragment() {} 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     mImageNum = getArguments() != null ? getArguments().getInt(IMAGE_DATA_EXTRA) : -1; 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 

     // image_detail_fragment.xml contains just an ImageView 
     final View v = inflater.inflate(R.layout.image_detail_fragment, container, false); 
     mImageView = (ImageView) v.findViewById(R.id.imageView); 
     return v; 
    } 

    @Override 
    public void onActivityCreated(Bundle savedInstanceState) { 
     super.onActivityCreated(savedInstanceState); 
     final int resId = MainActivity.imageResIds[mImageNum]; 
     mImageView.setImageResource(resId); // Load image into ImageView 
     addListenerOnImage(); 
    } 

    public void addListenerOnImage() { 
     mImageView.setOnClickListener(new OnClickListener() { 

      public void onClick(View arg0) { 
       //String id = String.valueOf(mImageNum); 
       String screen = null; 
       //final int resId = MainActivity.imageResIdsRed[mImageNum]; 
       //mImageView.setImageResource(resId); // Load image into ImageView 
       LinearLayout layout = new LinearLayout(getActivity().getApplication()); 
       getActivity(); 
       LayoutInflater layoutInflater = (LayoutInflater) getActivity().getBaseContext().getSystemService(FragmentActivity.LAYOUT_INFLATER_SERVICE); 
       View popupView = null; 
       Button btnDismiss = null; 
       Button btnAccept = null; 
       switch (mImageNum) { 
       case 0: screen = "ROCK"; 
         popupView = layoutInflater.inflate(R.layout.submit_rock_hand, null); 
         btnDismiss = (Button) popupView.findViewById(R.id.decline_rock_selection); 
         btnAccept = (Button) popupView.findViewById(R.id.accept_rock_selection); 
        try { 
         openServer(screen); 
        } catch (IOException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } 
        break; 
       case 1: screen = "PAPER"; 
         popupView = layoutInflater.inflate(R.layout.submit_paper_hand, null); 
         btnDismiss = (Button) popupView.findViewById(R.id.decline_paper_selection); 
         btnAccept = (Button) popupView.findViewById(R.id.accept_paper_selection); 
        try { 
         findServer(screen); 
        } catch (IOException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } 
        break; 
       case 2: screen = "SCISSORS"; 
         popupView = layoutInflater.inflate(R.layout.submit_scissors_hand, null); 
         btnDismiss = (Button) popupView.findViewById(R.id.decline_scissors_selection); 
         btnAccept = (Button) popupView.findViewById(R.id.accept_scissors_selection); 
        break; 
       case 3: screen = "LIZARD"; 
         popupView = layoutInflater.inflate(R.layout.submit_lizard_hand, null); 
         btnDismiss = (Button) popupView.findViewById(R.id.decline_lizard_selection); 
         btnAccept = (Button) popupView.findViewById(R.id.accept_lizard_selection); 
        break; 
       case 4: screen = "SPOCK"; 
         popupView = layoutInflater.inflate(R.layout.submit_spock_hand, null); 
         btnDismiss = (Button) popupView.findViewById(R.id.decline_spock_selection); 
         btnAccept = (Button) popupView.findViewById(R.id.accept_spock_selection); 
        break; 
       default: screen = "ERROR!"; 
        break; 
       } 

       final PopupWindow popupWindow = new PopupWindow(popupView,LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
       popupWindow.showAtLocation(layout, Gravity.CENTER, 0, 0); 
       btnDismiss.setOnClickListener(new Button.OnClickListener() { 

        public void onClick(View v) { 
         popupWindow.dismiss(); 
        } 
       }); 
       btnAccept.setOnClickListener(new Button.OnClickListener() { 

        public void onClick(View v) { 
         /*Insert what the accept button does here*/ 

        } 
       }); 

       //Toast.makeText(getActivity().getApplication(), 
       //  "test goes here", 
       //  Toast.LENGTH_SHORT).show(); 
      } 
     }); 
    } 

public void openServer(String hand) throws IOException { 
     // YEAHHHH TOAST!!! 



     Toast.makeText(getActivity().getApplication(),"Open Serverbf",Toast.LENGTH_SHORT).show(); 

     serverSocket = new ServerSocket(53000); 
     Socket clientSocket = null; 
     boolean end = false; 
     while (!end){ 
      Toast.makeText(getActivity().getApplication(),"Open Serveraf",Toast.LENGTH_SHORT).show(); 
      clientSocket = serverSocket.accept(); 
      end = serverSocket.isBound(); 
     }  

     Toast.makeText(getActivity().getApplication(),"Open Serveraftr",Toast.LENGTH_SHORT).show(); 

     PrintWriter out = new PrintWriter(clientSocket.getOutputStream()); 
     BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); 

     theirHand = in.readLine(); 
     myHand = hand; 
     out.println(hand); 
     Toast.makeText(getActivity().getApplication(),"Their Hand:" + theirHand,Toast.LENGTH_SHORT).show(); 
     Toast.makeText(getActivity().getApplication(),"My Hand:" + myHand,Toast.LENGTH_SHORT).show(); 

     out.close(); 
     in.close(); 
     clientSocket.close(); 
     serverSocket.close(); 

    } 

    public void findServer(String hand) throws IOException { 
     // YEAHHHH TOAST!!! 
     Toast.makeText(getActivity().getApplication(), "Find Server", Toast.LENGTH_SHORT).show(); 
     BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in)); 

     Socket kkSocket = null; 
     PrintWriter out = null; 
     BufferedReader in = null; 

     InetAddress duke = InetAddress.getByName("192.168.1.137"); 

     try { 
      kkSocket = new Socket(duke, 53000); 
      out = new PrintWriter(kkSocket.getOutputStream(), true); 
      in = new BufferedReader(new InputStreamReader(kkSocket.getInputStream())); 
     } catch (UnknownHostException e) { 
       System.err.println("could not connect"); 
      e.printStackTrace(); 
     } catch (IOException e) { 
       System.err.println("other error"); 
      e.printStackTrace(); 
     } 


     //out = new PrintWriter(kkSocket.getOutputStream(), true); 

     //in = new BufferedReader(new InputStreamReader(kkSocket.getInputStream())); 

     //BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in)); 

     //out.println(hand); 
     //myHand = hand; 
     //theirHand = in.readLine(); 
     //Toast.makeText(getActivity().getApplication(),"Their Hand:" + theirHand,Toast.LENGTH_SHORT).show(); 
     Toast.makeText(getActivity().getApplication(),"My Hand:",Toast.LENGTH_SHORT).show(); 

     out.close(); 
     in.close(); 
     //stdIn.close(); 
     kkSocket.close(); 
    } 

} 
+0

使用try-catch語句來捕獲和顯示正在發生的任何錯誤,並縮小問題的位置。 – mittmemo

+0

定義「它只是崩潰」。當你遇到異常時,不要自己編寫自己的消息,特別是那些像「其他錯誤」一樣平庸和無內容的消息。打印異常附帶的消息,最好是堆棧跟蹤。否則,你不知道發生了什麼問題,因爲你不知道。 – EJP

回答

1

您能告訴我們日誌嗎?什麼是錯誤?

二:當你這樣做:

while (!end){ 
     Toast.makeText(getActivity().getApplication(),"Open Serveraf",Toast.LENGTH_SHORT).show(); 
     clientSocket = serverSocket.accept(); 
     end = serverSocket.isBound(); 
    }  

接受你的服務器套接字將不再聽,因爲當條件不成立的第一個連接後。