我有一个非常简单的服务器,它作为 Java 应用程序运行(它是 echoServer 示例)。此外,我将客户端作为 android 应用程序运行,但它甚至没有连接到服务器......当我运行调试时,它似乎找不到 java.net.socket 库......它抛出异常“源不成立”。如何在android中添加socket库?你认为我还应该尝试什么?我只希望客户端连接到服务器,但它总是说不幸的是 socketclient 已停止。太感谢了!
客户端代码为:
public class SocketClient extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
Button.OnClickListener buttonSendOnClickListener
= new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Socket socket = null;
DataOutputStream dataOutputStream = null;
DataInputStream dataInputStream = null;
try {
**socket = new Socket("localhost", 4444);**
Log.i("client", "connected!");
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}