现在的位置: 首页 > 综合 > 正文

Android与PC互连聊天

2013年06月01日 ⁄ 综合 ⁄ 共 3512字 ⁄ 字号 评论关闭
服务端在pc机上
 
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.net.*;
import javax.swing.*;
public class Server extends JFrame implements ActionListener{
	JPanel jp;
	JButton jb;
	JTextField jtf;
	JTextArea jta;
	JScrollPane jsp;
	ServerSocket server;   
    Socket client;   
    BufferedReader in;   
    PrintWriter out; 
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new Server();
	}
	public Server(){
		jp=new JPanel();
		jb=new JButton("发送");
		jb.addActionListener(this);
		jtf=new JTextField(20);
		jp.add(jtf);
		jp.add(jb);
		jta=new JTextArea();
		jsp=new JScrollPane(jta);
		this.add(jsp);
		this.add(jp,"South");
		this.setTitle("服务端");
		this.setSize(400, 300);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);
		try {
			server = new ServerSocket(4700);
			client = server.accept();
			out = new PrintWriter(client.getOutputStream());
			in = new BufferedReader(new InputStreamReader(
					client.getInputStream()));

			while (true) {
				String line = in.readLine();
				if (line != null) {
					jta.append(line+"\r\n");
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} 
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		String info=jtf.getText().toString();
		out.println(info);
		out.flush();
	}
}

android客户端

 

import java.io.BufferedReader;   
import java.io.IOException;   
import java.io.InputStreamReader;   
import java.io.PrintWriter;   
import java.net.Socket;      
import android.app.Activity;      
import android.os.Bundle;   
import android.os.Handler;    
import android.os.Message;
import android.view.View;   
import android.view.View.OnClickListener;   
import android.widget.Button;   
import android.widget.EditText;  
public class Client extends Activity implements OnClickListener,Runnable {   
    EditText show; 
    EditText msg;
    Button send;     
    Socket client; 
    BufferedReader in;  
    PrintWriter out; 
    /** Called when the activity is first created. */  
    @Override  
    public void onCreate(Bundle savedInstanceState) {   
        super.onCreate(savedInstanceState); 
        try {  
            Socket s = new Socket("192.168.0.12",4700);  
            System.out.println("客户连接服务器成功");  
            out = new PrintWriter(s.getOutputStream());  
            in = new BufferedReader(new InputStreamReader(s.getInputStream()));
        } catch (Exception e) {  
            e.printStackTrace();  
        }   
        new Thread(this).start();
        setContentView(R.layout.main);  
        show = (EditText) findViewById(R.id.show);   
        msg = (EditText) findViewById(R.id.msg);   
        send = (Button) findViewById(R.id.send);   
        send.setOnClickListener(this);   
    }   
    @Override  
    public void onClick(View arg0) {   
        String text=msg.getText().toString();  
        msg.setText("");  
        out.println(text);  
        out.flush(); 
    }  
    Handler h=new Handler(){
    	public void handleMessage(Message msg) {  
    		String message = (String) msg.obj;   
           //System.out.println("Handler:"+message);   
            show.append("\n"+message); 
    	}
    };
	@Override
	public void run() {
		// TODO Auto-generated method stub
		try {  
            while (true) {  
                String str = in.readLine();  
                if (str != null) {  
                    Message m=new Message();
                    m.obj=str;
                    h.sendMessage(m); 
                }  
            }  
        } catch (IOException e) {  
            e.printStackTrace();  
        } 
	}
}  

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <TextView
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    android:text="显示文字" 
    />
	<EditText 
	android:id="@+id/show" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    />
    <TextView
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    android:text="输入文字" 
    />
    <EditText 
	android:id="@+id/msg" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    />
    <Button 
	android:id="@+id/send" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="发送"
    />
</LinearLayout>

 

 

添加访问网络权限

<uses-permission android:name="android.permission.INTERNET"/>

 

 

抱歉!评论已关闭.