c# - Multiple socket connection -
i have 6 devices, price checkers. have project client in supermarket scan product on device , return price of product. have managed connect 1 device, bar code , send data. can not seem connect multiple socket connection. code when receive , send data:
connecting 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 barcode:
private void getbarcode() { byte[] data = new byte[1024]; int receiveddatalength = lidhje.receive(data); numbercodebar = encoding.ascii.getstring(data, 0, receiveddatalength); }
sending data 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 connect multiple decives in same time, , when scans barcode device software return device price. have searched google, here on stackoverflow , codeproject no luck. sample code appreciate it. thanks.
yes can create multiple sockets in order communicate clients concurrently. see associated links sample code.
whether or not application client or server depend on how price checker expects communicated with.
to make life easier, focus on making synchronous solution work first. should use asynchronous code when understand how works.
Comments
Post a Comment