Socket 通信是很多应用的基础,因为工作上需要,我也研究了一下Socket通信,并不是想象中那么难。

以下是代码:

——————–socket- server——-C#-.Net——服务器端————-

using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;

namespace Server_CS
{
class Program
{
const int portNo = 14562;
static void Main(string[] args)
{
System.Net.IPAddress localAdd = System.Net.IPAddress.Parse("192.168.1.2");  ////注意这是你电脑(服务器)的ip,如果你要在模拟器上测试,服务器ip是127.0.0.1
TcpListener listener = new TcpListener(localAdd, portNo);
listener.Start();
TcpClient tcpClient = listener.AcceptTcpClient();
NetworkStream ns = tcpClient.GetStream();
byte[] data = new byte[tcpClient.ReceiveBufferSize];
int numBytesRead = ns.Read(data, 0, System.Convert.ToInt32(tcpClient.ReceiveBufferSize));
Console.WriteLine("收到 :" + Encoding.ASCII.GetString(data, 0, numBytesRead));
Console.ReadLine();
}
}
}  

——————-socket—java-android—-client——–客户端———-

package com.mazingtech.remotedesktop;
import java.io.BufferedWriter;
import java.io.DataOutputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

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

Button btn = (Button)findViewById(R.id.button1);

btn.setOnClickListener(new View.OnClickListener() {

public void onClick(View arg0) {

// TODO Auto-generated method stub

try {
EditText ipText =(EditText)findViewById(R.id.in_ip);
String ip = ipText.getText().toString();

EditText msgText =(EditText)findViewById(R.id.editText1);
String msg = msgText.getText().toString();

sendmsg(ip,msg);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}

public void sendmsg(String ip, String msg) throws Exception{
InetAddress serverAddr = InetAddress.getByName(ip);
//  我这个例子中ip是界面输入的,事实上如果在模拟器上运行访问主机的服务,你只能输入10.0.2.2这个ip这时你服务器ip要相应的设为127.0.0.1,
//但是如果你是部署到真机,则是可以输入真实ip 如192.168.1.2, 当然的你服务器设置的ip也必须是这个
//这里说的真实ip是指局域网内的ip。这个例子还没涉及到外网的socket访问,那些访问要涉及到防火墙和NAT

Toast.makeText(RDesktop.this, " 连接中...", Toast.LENGTH_LONG).show();
Socket socket = new Socket(serverAddr, 500);
String message = msg;
try {
Toast.makeText(RDesktop.this, "发送消息...", Toast.LENGTH_LONG).show();

/////方法一

PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
out.println(message);

////方法二   两个方法都可以发送,但是同时执行这两个方法,只有一次发送被接收到,因为上面的c#服务器例子中 只接收一次

/*OutputStream ops=socket.getOutputStream();
DataOutputStream dos=new DataOutputStream(ops);
dos.write(message.getBytes());
dos.close();

*/

 

Toast.makeText(RDesktop.this, "发送成功", Toast.LENGTH_LONG).show();

} catch (Exception e) {
Log.e("TCP错误:", "Erro", e);
Toast.makeText(RDesktop.this, e.toString(), Toast.LENGTH_LONG).show();

} finally {
socket.close();
}

}
}

运行实例
Android 与电脑 socket 基本通信实例
你可以看到,socket通信很快(当然这测试传输的字节少的要命),android连接提示还没消去,控制台就受到信息了。
希望能给你有帮助

1.00 平均分 (23%) - 12