下面是主线程部分代码
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main__client);
scroll_text = (EditText)findViewById(R.id.ScrollEdit);//构建一个滚动区域编辑的文本对象
edit_text = (EditText)findViewById(R.id.EditText_Send);//构建一个输入信息的文本对象
edit_text_IP = (EditText)findViewById(R.id.EditText_Ip);//构建一个编辑IP文本的对象
edit_text_PORT = (EditText)findViewById(R.id.EditText_Port);//构建一个编辑端口的文本对象
connect_button = (Button)findViewById(R.id.Button_Connect);//构建一个按钮对象
disconnect_button = (Button)findViewById(R.id.Button_Disconnect);//构建一个按钮对象
send_button = (Button)findViewById(R.id.Button_Send);//构建一个按钮对象
/* Thread_Send urgent_send = this.new Thread_Send();//实例化心跳数据发送线程类
Thread_Recive urgent_recive =this.new Thread_Recive();//实例化心跳数据接收线程类
*/
handler = new Handler(){//创建一个Handler实例进行套接字任务的传递
public void handleMessage(Message msg){
//如果消息来自子线程
if(msg.what == 0x04)
{
//将读取的内容显示在滚动框内
try{
scroll_text.append(msg.obj.toString()+"\n");
}catch(Exception e){
e.printStackTrace();
}
}
}
};
//连接按钮监听器
connect_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(!(Main_ClientActivity.edit_text_IP.getText().toString().equals("")) && !(Main_ClientActivity.edit_text_PORT.getText().toString().equals(""))){
new Thread(connectthread).start();
}
}
});
//断开连接按钮监听器
disconnect_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
new Thread(disconnectthread).start();
}
});
//发送文本按钮监听器
send_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try{
//当用户点击发送按钮时,开启发送线程,将用户数据包装成Message后发送给子线程
Message msg = new Message();
msg.what = 0x03;
msg.obj = edit_text.getText().toString();
sendthread.sendhandler.sendMessage(msg);
edit_text.setText("");
new Thread(sendthread).start();
}catch(Exception e){
e.printStackTrace();
}
}
});
/*new Thread(recivethread).start();*/
new Thread(connectthread).start();
}
下面是子线程连接代码
public class connectThread implements Runnable{
@Override
public void run(){
try{
if(!(Main_ClientActivity.edit_text_IP.getText().toString().equals("")) || (Main_ClientActivity.edit_text_PORT.getText().toString().equals(""))){
Main_ClientActivity.scroll_text.append("连接中...\n");
Main_ClientActivity.socket.setSoTimeout(5000);
/*Main_ClientActivity.socket = new Socket(Main_ClientActivity.edit_text_IP.getText().toString(),Integer.valueOf(Main_ClientActivity.edit_text_PORT.getText().toString()));//创建套接字通讯
*//*Main_ClientActivity.socket = new Socket();
Main_ClientActivity.address = new InetSocketAddress(Main_ClientActivity.edit_text_IP.getText().toString(), Integer.valueOf(Main_ClientActivity.edit_text_PORT.getText().toString()));
Main_ClientActivity.socket.connect(Main_ClientActivity.address,5000);*/
Main_ClientActivity.socket = new Socket("192.168.1.105",10002);
Main_ClientActivity.scroll_text.append("连接中1...\n");
Main_ClientActivity.out = new ObjectOutputStream(Main_ClientActivity.socket.getOutputStream());//创建输出流
Main_ClientActivity.in = new ObjectInputStream(Main_ClientActivity.socket.getInputStream());//创建输入流
if(Main_ClientActivity.socket.isConnected()){
Main_ClientActivity.scroll_text.append("连接至:"+Main_ClientActivity.socket.getInetAddress().getHostName()+"\n");
Main_ClientActivity.scroll_text.append("------------------------\n");
Main_ClientActivity.connect_button.setEnabled(false);
Main_ClientActivity.disconnect_button.setEnabled(true);
Main_ClientActivity.send_button.setEnabled(true);
}
else{
Main_ClientActivity.scroll_text.append("连接失败! 请检验IP地址和端口是否正确!\n");
Main_ClientActivity.scroll_text.append("------------------------");
Main_ClientActivity.socket.close();
Main_ClientActivity.out.close();
Main_ClientActivity.in.close();
Main_ClientActivity.connect_button.setEnabled(true);
Main_ClientActivity.disconnect_button.setEnabled(false);
Main_ClientActivity.send_button.setEnabled(false);
}
}
}
catch(SocketTimeoutException soc){
Main_ClientActivity.scroll_text.append("连接超时!请检查IP和端口后重新连接!\n");
soc.printStackTrace();
}
catch(Exception e){
e.printStackTrace();
}
}
}
帮忙看看啊 大神们,卡了好几天了。。。IP端口设置都对,也给了网络权限,也设置子线程连接
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main__client);
scroll_text = (EditText)findViewById(R.id.ScrollEdit);//构建一个滚动区域编辑的文本对象
edit_text = (EditText)findViewById(R.id.EditText_Send);//构建一个输入信息的文本对象
edit_text_IP = (EditText)findViewById(R.id.EditText_Ip);//构建一个编辑IP文本的对象
edit_text_PORT = (EditText)findViewById(R.id.EditText_Port);//构建一个编辑端口的文本对象
connect_button = (Button)findViewById(R.id.Button_Connect);//构建一个按钮对象
disconnect_button = (Button)findViewById(R.id.Button_Disconnect);//构建一个按钮对象
send_button = (Button)findViewById(R.id.Button_Send);//构建一个按钮对象
/* Thread_Send urgent_send = this.new Thread_Send();//实例化心跳数据发送线程类
Thread_Recive urgent_recive =this.new Thread_Recive();//实例化心跳数据接收线程类
*/
handler = new Handler(){//创建一个Handler实例进行套接字任务的传递
public void handleMessage(Message msg){
//如果消息来自子线程
if(msg.what == 0x04)
{
//将读取的内容显示在滚动框内
try{
scroll_text.append(msg.obj.toString()+"\n");
}catch(Exception e){
e.printStackTrace();
}
}
}
};
//连接按钮监听器
connect_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(!(Main_ClientActivity.edit_text_IP.getText().toString().equals("")) && !(Main_ClientActivity.edit_text_PORT.getText().toString().equals(""))){
new Thread(connectthread).start();
}
}
});
//断开连接按钮监听器
disconnect_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
new Thread(disconnectthread).start();
}
});
//发送文本按钮监听器
send_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try{
//当用户点击发送按钮时,开启发送线程,将用户数据包装成Message后发送给子线程
Message msg = new Message();
msg.what = 0x03;
msg.obj = edit_text.getText().toString();
sendthread.sendhandler.sendMessage(msg);
edit_text.setText("");
new Thread(sendthread).start();
}catch(Exception e){
e.printStackTrace();
}
}
});
/*new Thread(recivethread).start();*/
new Thread(connectthread).start();
}
下面是子线程连接代码
public class connectThread implements Runnable{
@Override
public void run(){
try{
if(!(Main_ClientActivity.edit_text_IP.getText().toString().equals("")) || (Main_ClientActivity.edit_text_PORT.getText().toString().equals(""))){
Main_ClientActivity.scroll_text.append("连接中...\n");
Main_ClientActivity.socket.setSoTimeout(5000);
/*Main_ClientActivity.socket = new Socket(Main_ClientActivity.edit_text_IP.getText().toString(),Integer.valueOf(Main_ClientActivity.edit_text_PORT.getText().toString()));//创建套接字通讯
*//*Main_ClientActivity.socket = new Socket();
Main_ClientActivity.address = new InetSocketAddress(Main_ClientActivity.edit_text_IP.getText().toString(), Integer.valueOf(Main_ClientActivity.edit_text_PORT.getText().toString()));
Main_ClientActivity.socket.connect(Main_ClientActivity.address,5000);*/
Main_ClientActivity.socket = new Socket("192.168.1.105",10002);
Main_ClientActivity.scroll_text.append("连接中1...\n");
Main_ClientActivity.out = new ObjectOutputStream(Main_ClientActivity.socket.getOutputStream());//创建输出流
Main_ClientActivity.in = new ObjectInputStream(Main_ClientActivity.socket.getInputStream());//创建输入流
if(Main_ClientActivity.socket.isConnected()){
Main_ClientActivity.scroll_text.append("连接至:"+Main_ClientActivity.socket.getInetAddress().getHostName()+"\n");
Main_ClientActivity.scroll_text.append("------------------------\n");
Main_ClientActivity.connect_button.setEnabled(false);
Main_ClientActivity.disconnect_button.setEnabled(true);
Main_ClientActivity.send_button.setEnabled(true);
}
else{
Main_ClientActivity.scroll_text.append("连接失败! 请检验IP地址和端口是否正确!\n");
Main_ClientActivity.scroll_text.append("------------------------");
Main_ClientActivity.socket.close();
Main_ClientActivity.out.close();
Main_ClientActivity.in.close();
Main_ClientActivity.connect_button.setEnabled(true);
Main_ClientActivity.disconnect_button.setEnabled(false);
Main_ClientActivity.send_button.setEnabled(false);
}
}
}
catch(SocketTimeoutException soc){
Main_ClientActivity.scroll_text.append("连接超时!请检查IP和端口后重新连接!\n");
soc.printStackTrace();
}
catch(Exception e){
e.printStackTrace();
}
}
}
帮忙看看啊 大神们,卡了好几天了。。。IP端口设置都对,也给了网络权限,也设置子线程连接