eclipse juno 电脑是jre1.8 手机是安卓4.1.2
服务器到接受信息卡住,手机到连接服务器卡住
没报错
我试了手机连电脑开的wifi和手机电脑连一个wifi都不行
不知是哪出了问题?
刚注册的没悬赏见谅
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1=(Button)findViewById(R.id.button1);
editText1=(EditText)findViewById(R.id.editText1);
editText2=(EditText)findViewById(R.id.editText2);
/* // 开启服务器
ExecutorService exec = Executors.newCachedThreadPool();
UDPServer server = new UDPServer();
exec.execute(server);*/
// int port = 5050;
new AlertDialog.Builder(MainActivity.this).setTitle("测试").setMessage("创建成功").setPositiveButton("确定", null).show();
try
{
addr = InetAddress.getByName("192.168.191.4");
new AlertDialog.Builder(MainActivity.this).setTitle("测试").setMessage("ip").setPositiveButton("确定", null).show();
// dClient = new DatagramSocket(PORT,addr);
dClient = new DatagramSocket(); //随机端口
new AlertDialog.Builder(MainActivity.this).setTitle("测试").setMessage("连接 UDP 成功").setPositiveButton("确定", null).show();
button1.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
String str1 = editText1.getText().toString();
String str2 = editText2.getText().toString();
Pattern p = Pattern.compile("[^( |\\n)*]"); //匹配 除了空白以外的字符
Matcher m = p.matcher(str2);
if(m.find()==true) //不能只含有空格或者回车
{
/*String msg;
try
{
Socket socket = new Socket("202.204.48.135",5554);
PrintWriter out = new PrintWriter( new BufferedWriter( new OutputStreamWriter(socket.getOutputStream())),true);
out.println(str2);
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
msg = br.readLine();*/
str2="ID1:\n"+str2;
// str1="frag";
// editText1.setText(str1);
DatagramPacket sendPacket = new DatagramPacket(str2.getBytes() ,str2.length() , addr , PORT);
try
{
dClient.send(sendPacket);
} catch (IOException e)
{
// TODO 自动生成的 catch 块
e.printStackTrace();
}
// str1=str1+"\n"+"ID1:\n"+str2+"\n";
// editText1.setText(str1);
editText2.setText(null);
/* } catch (IOException e)
{
// TODO 自动生成的 catch 块
e.printStackTrace();
} */
}
else
{
editText2.setText(null);
}
}
});
} catch (UnknownHostException e)
{
// TODO 自动生成的 catch 块
e.printStackTrace();
} catch (SocketException e)
{
// TODO 自动生成的 catch 块
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;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}