PDA与PC基于Socket的同步
2006-12-20 20:44
302 查看
示例代码如下:
PDA端:
string msg = "PDA_Ready";
private string SendMsgAndReciveMsg(string msg)
{
// 需要返回的字符串
string result = string.Empty;
try
{
byte[] sendmsg = System.Text.Encoding.UTF8.GetBytes(msg); 需要发送的信息
System.Net.Sockets.TcpClient tcpc = new TcpClient();
System.Net.IPAddress ip = System.Net.IPAddress.Parse(this.hostIP); IP地址字符串转换在IP地址
System.Net.IPEndPoint ipendpoint = new System.Net.IPEndPoint(ip,this.port); 建立网络连接端点
tcpc.Connect(ipendpoint);
tcpc.GetStream().Write(sendmsg,0,sendmsg.Length); 发送信息到PC
int i = tcpc.GetStream().Read(request,0,request.Length);读取信息
result = Encoding.UTF8.GetString(request,0,i);
tcpc.Close();
return result;
}
catch(Exception ex)
{
return result;
}
}
PC端:
启动一个线程:
private System.Net.Sockets.TcpListener tcpl;
private System.Threading.Thread thread;
thread=new Thread(new ThreadStart(SendReceive));
thread.Priority=System.Threading.ThreadPriority.AboveNormal;
thread.Start();
接收与发送
private void SendReceive()
{
try
{
ArrayList al=new ArrayList();
tcpl.Start();
this.statusBarPanel1.Text = "正在监听...";
while(lisen)
{
TcpClient tcpc = tcpl.AcceptTcpClient();
NetworkStream tcpStream = tcpc.GetStream();
byte[] request=new byte[6144];
int bytesRead = tcpStream.Read(request,0,request.Length);
sbReceive = Encoding.UTF8.GetString(request,0,bytesRead);
switch(sbReceive)
{
case "PDA_Ready":
byte[] msg = Encoding.UTF8.GetBytes("Server_Ready");
tcpStream.Write(msg,0,msg.Length);
break;
}
}
PDA端:
string msg = "PDA_Ready";
private string SendMsgAndReciveMsg(string msg)
{
// 需要返回的字符串
string result = string.Empty;
try
{
byte[] sendmsg = System.Text.Encoding.UTF8.GetBytes(msg); 需要发送的信息
System.Net.Sockets.TcpClient tcpc = new TcpClient();
System.Net.IPAddress ip = System.Net.IPAddress.Parse(this.hostIP); IP地址字符串转换在IP地址
System.Net.IPEndPoint ipendpoint = new System.Net.IPEndPoint(ip,this.port); 建立网络连接端点
tcpc.Connect(ipendpoint);
tcpc.GetStream().Write(sendmsg,0,sendmsg.Length); 发送信息到PC
int i = tcpc.GetStream().Read(request,0,request.Length);读取信息
result = Encoding.UTF8.GetString(request,0,i);
tcpc.Close();
return result;
}
catch(Exception ex)
{
return result;
}
}
PC端:
启动一个线程:
private System.Net.Sockets.TcpListener tcpl;
private System.Threading.Thread thread;
thread=new Thread(new ThreadStart(SendReceive));
thread.Priority=System.Threading.ThreadPriority.AboveNormal;
thread.Start();
接收与发送
private void SendReceive()
{
try
{
ArrayList al=new ArrayList();
tcpl.Start();
this.statusBarPanel1.Text = "正在监听...";
while(lisen)
{
TcpClient tcpc = tcpl.AcceptTcpClient();
NetworkStream tcpStream = tcpc.GetStream();
byte[] request=new byte[6144];
int bytesRead = tcpStream.Read(request,0,request.Length);
sbReceive = Encoding.UTF8.GetString(request,0,bytesRead);
switch(sbReceive)
{
case "PDA_Ready":
byte[] msg = Encoding.UTF8.GetBytes("Server_Ready");
tcpStream.Write(msg,0,msg.Length);
break;
}
}
相关文章推荐
- 基于Socket编程的远程控制PC音乐播放器App(二)
- [转载]pda 和pc 之间的文件操作(利用Microsoft ActiveSync同步文件数据)
- 基于python的PC文件自动同步系统
- Visual Basic Winsock API操作模块(基于API方式的socket同步阻塞通讯类)
- [20180313智慧餐厅推荐系统02]基于python的socket编程代码,实现PC与服务器的简单通信
- [转载]pda 和pc 之间的文件操作(利用Microsoft ActiveSync同步文件数据)
- 透过 Socket API 让 PDA 和远程 PC 联机
- 基于Socket编程的远程控制PC音乐播放器App(一)
- 基于C#的socket编程的TCP同步实现
- 基于C#的Socket同步和异步通信(TCP/UDP)【入门推荐】
- 如何把PDA中文本文档导出到电脑PC中?基于Java平台
- 基于C#的同步Socket程序(TCP)
- 【转载】基于C#的socket编程的TCP同步实现
- 基于C#的socket编程的TCP同步实现
- Android自学笔记(二)基于Socket实现Android与PC通信
- 基于Socket的Android与PC简单聊天应用的实现
- Socket中阻塞与非阻塞,同步与异步
- 基于Unity的游戏项目客户端服务器寻路同步方案
- 基于rsync+inotify实现数据实时同步传输
- 使用RAPI对WM与PC进行同步通讯