我正在尝试开发一个基本的应用程序,用于从Android客户端向PC服务器发送字符串,但问题是服务器没有收到任何消息。我正在使用套接字在客户端和服务器之间进行通信。服务器已启动但并没有收到任何消息。 以下是用java编写的示例服务器代码
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package wifi.communicate;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
public class WIfiCommunicate
{
private static final int port=4444;
private static ServerSocket serversocket;
private static Socket clientsocket;
private static InputStreamReader reader;
private static BufferedReader breader;
private static InputStreamReader InputStreamReader;
private static String message;
public static void main(String[] args)
{
try
{
serversocket=new ServerSocket(port,0,InetAddress.getLocalHost());
System.out.println("IP: "+serversocket.getInetAddress()+ "port" +serversocket.getLocalPort());
}
catch(IOException e)
{
System.out.println("Could not listen to port: "+port);
}
System.out.println("Server started ");
while(true)
{
try
{
clientsocket=serversocket.accept();
reader=new InputStreamReader(clientsocket.getInputStream());
breader=new BufferedReader(reader);
message=breader.readLine();
}
catch(IOException e)
{
System.out.println("Message not received");
}
}
}
}
here is the android activity code
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends Activity {
EditText editText;
String n1;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText=(EditText)findViewById(R.id.editText1);
}
public void onClick(View view)
{
if(view.getId()==R.id.button1)
{
n1=editText.getText().toString();
new connectserver().execute(n1);
}
}
public class connectserver extends AsyncTask<String,Integer,Socket>
{
private static final String IP_address="192.168.0.101";
private static final int port=4444;
private String mText;
protected Socket doInBackground(String... params)
{
if(params.length!=1)
throw new IllegalArgumentException();
mText=params[0];
Socket client=null;
try
{
client=new Socket(IP_address,port);
}
catch(UnknownHostException e)
{
e.printStackTrace();
}
catch(IOException e)
{
e.printStackTrace();
}
return client;
}
protected void onPostExecute(Socket client)
{
try
{
PrintWriter pwriter;
String message=mText;
editText.setText("");
pwriter=new PrintWriter(client.getOutputStream(),true);
pwriter.write(message);
pwriter.flush();
pwriter.close();
client.close();
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}