Android客户端连接PC服务器端(Socket连接)

客户端代码:
package cn.com;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
import android.view.View;
import android.view.View.OnClickListener;

public class SocketClient extends Activity implements OnClickListener{

	Socket socket;
	DataInputStream dis;
	DataOutputStream dos;

	private TextView mTextView1;
	private Button Button01;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		mTextView1 = (TextView) findViewById(R.id.rec);
		
		Button01 = (Button) findViewById(R.id.Button01);
		Button01.setOnClickListener(this);

		Client("127.0.0.1");
		//WriteInt(25);
		//WriteString("client");
		//ReadInt();
	}

	public void Client(String IP) {
		try {
			// specified by the parameters dstName and dstPort.
			// 创建一个socket流连接到目标主机,
			socket = new Socket(IP, 10000);
			// 输入流 读出数据 输出流写数据
			dis = new DataInputStream(socket.getInputStream());
			dos = new DataOutputStream(socket.getOutputStream());
		} catch (IOException ioe) {
			ioe.printStackTrace();
		}
	}

	// 写数据到socket
	public void WriteInt(int i) {
		try {
			dos.writeInt(i);
			dos.flush();
		} catch (IOException ioe) {
			ioe.printStackTrace();
		}
	}
	
	public void WriteString(String str){
		StringBuffer message = new StringBuffer();
		message.append(str);
		byte[] b = new byte[6];
		try {
			dos.write(12);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	// 显示从socket返回的数据
	public void ReadInt() {
		try {
			mTextView1.setText(dis.readInt());
			System.out.println(dis.readInt());
		} catch (IOException ioe) {
			ioe.printStackTrace();
		}
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		WriteInt(25);
	}

}



服务器端代码:
package cn.com;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {
	ServerSocket serversocket;
	Socket socket;

	DataInputStream dis;
	DataOutputStream dos;

	public Server() {
		try {
			serversocket = new ServerSocket(10000);
			System.out.println("等待 Client 連接");
			socket = serversocket.accept();
			System.out.println("Client 已連接");
			dis = new DataInputStream(socket.getInputStream());
			dos = new DataOutputStream(socket.getOutputStream());
		} catch (IOException ioe) {
			ioe.printStackTrace();
		}
	}

	public void WriteInt(int i) {
		try {
			dos.writeInt(i);
			dos.flush();
		} catch (IOException ioe) {
			ioe.printStackTrace();
		}
	}

	public void ReadInt() {
		try {
			System.out.println(dis.readInt());
		} catch (IOException ioe) {
			ioe.printStackTrace();
		}
	}

	public static void main(String args[]) {
		Server theServer = new Server();
		theServer.WriteInt(10);
		theServer.ReadInt();
	}
}



我调试的时候总是报空指针异常,大伙帮我看下是什么问题,谢谢。
注意在客户端的AndroidMenifest.xml中需要加
<uses-permission android:name="android.permission.INTERNET" />权限!

你可能感兴趣的:(java,.net,android,socket,dos)