【发布时间】:2013-08-27 05:40:09
【问题描述】:
客户端使用服务器接受的相同连接发送数据的方法是否正确?
情况是这样的,我的 PC 上运行着蓝牙服务器,而另一方面,我有带有客户端和服务器的 android 手机。从android端客户端开始连接。我正在使用来自 android 示例的蓝牙聊天示例。
Android 上的服务器-客户端看起来像
BluetoothSocket socket;
InputStream tmpIn = null;
OutputStream tmpOut = null;
// Get the BluetoothSocket input and output streams
tmpIn = socket.getInputStream();
tmpOut = socket.getOutputStream();
在 PC 端,我使用 Bluez 库来实现服务器和客户端。
该代码包括蓝牙接收线程和一个主线程,每当服务器接受来自安卓手机的连接时,我只需将套接字值分配给全局变量,并且每当客户端需要发送数据时,它使用同一个套接字发送,
服务器:-
int GLOBAL_CLIENT;
void* recive_bluetooth_trd(void*)
{
...............................
..............................
client = accept(s, (struct sockaddr *)&rem_addr, &opt);
GLOBAL_CLIENT=client;
while(1){
bytes_read = read(client, buf, sizeof(buf));
....................
...................
}
客户:-
void clinet(char *msg, int length){
........................
int bytes_write=write(GLOBAL_CLIENT,message, length);
..........................
}
我的问题是,这是一个正确的方法吗?问题是有时客户端从 PC 成功发送数据但在 android 端没有接收。
【问题讨论】:
标签: c++ c sockets bluetooth bluez