I am new to C# and am writing a program that will run on a pc and allow either another program or an external device, like a touchpanel control system, to connect to it. I have the rest of my code ready, but I can't figure out how to make the socket connection work.
I need the following
My application listens for a connection.
Device connects to my application.
the socket stays open until it is disconnected by the device.
The application must be able to send to or receive from the device at any time, full duplex. Neither operation can block or depend on the other.
This seems like something that would be common, but I can't seem to find a good enough example or thread here on Bytes to get me going in the right direction. Can anyone hep?
1 4300
After I posted, I figured it out - and, of course, it wasn't that hard. For anyone that got stuck like me, it is below. Any issues with doing it this way? - class Com
-
{
-
TcpListener tcpListener = new TcpListener(IPAddress.Loopback, 50000);
-
TcpClient tcpClient;
-
NetworkStream clientStream;
-
-
public Com()
-
{
-
tcpListener.Start();
-
tcpClient = tcpListener.AcceptTcpClient();
-
clientStream = tcpClient.GetStream();
-
Thread Reader = new Thread(new ThreadStart(SocketRead));
-
Reader.IsBackground = true;
-
Reader.Start();
-
Reader.Join();
-
}
-
-
private void SocketSend(string data)
-
{
-
-
try
-
{
-
ASCIIEncoding encoder = new ASCIIEncoding();
-
byte[] message = encoder.GetBytes(data);
-
clientStream.Write(message, 0, message.Length);
-
clientStream.Flush();
-
Console.WriteLine(data + " sent");
-
}
-
catch
-
{
-
-
}
-
return;
-
}
-
-
private void SocketRead()
-
{
-
bool listening = true;
-
while (listening)
-
{
-
byte[] message = new byte[4096];
-
int bytesRead;
-
while (true)
-
{
-
bytesRead = 0;
-
try
-
{
-
bytesRead = clientStream.Read(message, 0, 4096);
-
}
-
catch
-
{
-
break;
-
}
-
-
if (bytesRead == 0)
-
{
-
break;
-
}
-
ASCIIEncoding encoder = new ASCIIEncoding();
-
string received = encoder.GetString(message, 0, bytesRead);
-
Console.WriteLine(received + " received");
-
Thread Sender = new Thread(() => SocketSend(received));
-
Sender.Start();
-
}
-
}
-
}
-
}
and an easy client for it in Python -
import socket
-
-
class Client:
-
def __init__(self):
-
self.Sender()
-
-
def Sender(self):
-
host = 'localhost'
-
port = 50000
-
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
-
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
-
s.connect((host, port))
-
-
while True:
-
data = raw_input("Send to Server>> ")
-
s.send(data)
-
data = s.recv(1024)
-
if data == "exit":
-
break
-
print data
-
s.close()
-
-
Client()
-
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Brandon |
last post by:
Java seems to have taken off as the platform and language of choice
for many embedded devices. Would it be feasible for Python(perhaps
running on an embedded version of Linux) to act in such a...
|
by: Premshree Pillai |
last post by:
For the uninitiated: "Socket Programming in Python" at
evolt.org: http://evolt.org/article/Socket_Programming_in_Python/17/60276/index.html
=====
-Premshree
http://www.qiksearch.com/]
...
|
by: Tim Black |
last post by:
My application requires sending a large piece (~2MB) of data to
several devices on a network via TCP sockets. I have experimented with
different methods for doing this and this has raised some...
|
by: Sham Yemul |
last post by:
Hello,
I recently started development in Vb.net. I am about to finish a product in
VB.net Database Applicatio, Sql Server as Backend.
I read little about wireless technology and PDAs.
Is it...
|
by: Bud Cookson |
last post by:
We are in the process of developing the requirements for a new medical
device and the question has been raised about what development tool/language
should be used. The application(s) will be...
|
by: Macca |
last post by:
I am writing an application that uses asynchronous sockets to get data over
ethernet from embedded devices, up to 30 concurrent devices.(These devices
are written in C).
My application...
|
by: Macca |
last post by:
Hi,
My app has an asynchronous socket server to receive and transmit data over
ethernet connections coming in through a switch.
My App will handle concurrent data for possibly 20+ embedded C...
|
by: Lina |
last post by:
Hi all,
am doing Client/Server application using Socket class, then i need to
analysis the packet to retrieve the header for each packet, i read in some
resources that i can do it using...
|
by: Shoveler |
last post by:
I'm trying to send an ARP to determine different devices on the
network. I need to see the MAC address of my devices on my LAN. With
the MAC I can determine if I need to talk to that devices. I...
|
by: Hao |
last post by:
We are doing very intensive network communication. We collect data from
thousands of electric devices every minutes. The devices understand both
socket and web service. They are either embeded...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |