关于android socket通讯,求大神帮看看

dghjkoll dghjkoll 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
写了个android与pc端通信的例子,pc端用的是mfc写的,做服务端。想实现以下功能:
android填写完用户名和密码点击登录按钮,android把用户名和密码打包发给pc端,pc端收到后在发出一条消息给android,android收到并解析这条消息后启动一个新Activity。
现在的情况是android能给PC发信息,PC端给android发信息收不到。
这是android的源码:
package com.example.sendtoserver;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.UnknownHostException;


import android.os.Bundle;
import android.os.Message;
import android.os.StrictMode;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity
{
Socket socket=null;
String ip="192.168.137.1";
int port=8888;
BufferedReader br=null;
BufferedWriter bw=null;
Button loginButton=null;
EditText username=null;
EditText password=null;
ReceiveThread receThread=null;
String receString=null;
InitThread it=null;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setContentView(R.layout.activity_main);

loginButton=(Button)this.findViewById(R.id.login);
loginButton.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0)
{
// TODO Auto-generated method stub
if(bw!=null)
{
try
{
bw.write("/Cmd Login /username "+username.getText().toString()+" /password "+password.getText().toString()+" /");
bw.flush();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
username=(EditText)this.findViewById(R.id.username);
password=(EditText)this.findViewById(R.id.password);

it=new InitThread();
it.start();
receThread=new ReceiveThread();
receThread.start();
}
class InitThread extends Thread
{

@Override
public void run()
{
// TODO Auto-generated method stub
super.run();
try
{
socket=new Socket(ip,port);
bw=new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
br=new BufferedReader(new InputStreamReader(socket.getInputStream()));
}
catch (UnknownHostException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}
class ReceiveThread extends Thread
{

@Override
public void run()
{
// TODO Auto-generated method stub
super.run();
if(br!=null)
{
try
{
int count=0;
char[] buffer=null;
while(true)
{
if((count=br.read(buffer))>0)
{
break;
}
}
receString=buffer.toString();
String[] cmd=parse(receString);
Intent intent=new Intent();
intent.putExtra("id",(cmd[2].split(" "))[1]);
intent.putExtra("queueId",(cmd[3].split(" "))[1]);
intent.putExtra("queueName",(cmd[4].split(" "))[1]);
intent.setClass(MainActivity.this,ListActivity.class);
(MainActivity.this).startActivity(intent);
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
private String[] parse(String rece)
{
String[] elem=null;
elem=rece.split("/");
return elem;
}
}
@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
protected void onDestroy()
{
// TODO Auto-generated method stub
super.onDestroy();
if(socket!=null)
{
try
{
socket.close();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(br!=null)
{
try
{
br.close();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(bw!=null)
{
try
{
bw.close();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// it.destroy();
// receThread.destroy();
}


}
...全文

相关推荐

评论 8
收藏 已收藏
分享
返回顶部
dghjkoll
Android 80,490 成员 91,371 内容