多个插座连接 [英] Multiple socket connection
问题描述
您好,
我有6台设备,价格检查器。我有一个项目,超市的客户将扫描此设备上的产品,它将返回产品的价格。我已设法连接一台设备,获取条形码并发送数据。但我似乎无法连接多个插座连接。
这是我收到和发送数据时的代码:
连接使用该设备:
私人 无效 connect1( string adip, int porta)
{
try
{
IPEndPoint ip = new IPEndPoint(IPAddress.Parse(adip),porta);
connect1 = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
connect1.Connect(ip);
}
catch (System.Net.Sockets.SocketException se)
{
MessageBox.Show(se.Message) );
}
}
获取条形码:
私有 void getbarCode()
{
byte [] data = new byte [ 1024 跨度>];
int receivedDataLength = lidhje.Receive(data);
numberCodeBar = Encoding.ASCII.GetString(data, 0 ,receivedDataLength);
}
向设备发送数据:
< pre lang =cs> private void sendData( string price1)
{
尝试
{
对象 objData = price1;
byte [] price = System.Text.Encoding.ASCII.GetBytes(objData.ToString());
connect1.Send(price);
}
catch (System.Net.Sockets.SocketException se)
{
MessageBox.Show(se.Message) );
}
}
我想在同一时间使用muliple decives进行此操作。我搜索了谷歌,但没有运气。
我建议写一个代理服务/消息队列,它将以异步方式从设备收集产品扫描结果,然后将其提取到价格服务以返回适当的产品价格。
Hello,
I have 6 devices, price checkers. I have a project which the client in a supermarket will scan a product on this device and it will return the price of the product. I have managed to connect with one device, get the bar code and send data. But I can not seem to do to connect with multiple socket connection.
This is the code when I receive and send data:
Connecting with the device:
private void connect1(string adip, int porta)
{
try
{
IPEndPoint ip = new IPEndPoint(IPAddress.Parse(adip), porta);
connect1 = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
connect1.Connect(ip);
}
catch (System.Net.Sockets.SocketException se)
{
MessageBox.Show(se.Message);
}
}
Getting the barcode:
private void getbarCode()
{
byte[] data = new byte[1024];
int receivedDataLength = lidhje.Receive(data);
numberCodeBar = Encoding.ASCII.GetString(data, 0, receivedDataLength);
}
Sending data to device:
private void sendData(string price1)
{
try
{
Object objData = price1;
byte[] price = System.Text.Encoding.ASCII.GetBytes(objData.ToString());
connect1.Send(price);
}
catch (System.Net.Sockets.SocketException se)
{
MessageBox.Show(se.Message);
}
}
I want to make this with muliple decives in the same time. I have searched Google but no luck.
I would suggest to write down one broker service/ message queue which will collect product scan results from the devices in async manner and then fetch it to the price service to return appropriate price of product.
这篇关于多个插座连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!