Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I've searched over 2 weeks for samples or anything else for my question.
Found some topics and examples but can't use them ...
Here is the problem : I have a server-client application which the server have to listen all the time and client is sending continuously Images to the server.
I've done the send-and-done which means client send 1 image to the server and server receive the image and socket will be closed .
But i want to send continuously Images to the server ...
Found this question : Can a TCP c# client receive and send continuously/consecutively without sleep?
But can't use them and samples .
I know the basic TCP Socket Programming and Basics of Thread Programming , but can't implement this .
Sorry For my bad english !
=====edit
actually a timer captured a screenshot with 1 second interval and these captured images after a process for compressing are send to server .
its continuously sending (Just Sending).
Is there any sample or something that i can use ?
Thanks .
When sending a single message on a socket, you can just send the payload and close - and the receiver just has to read until the socket reports it has closed and they'll have the message; nice and simple, but it doesn't scale to multiple messages.
You can't just use the packet layout because TCP is implemented as a stream protocol not a packet protocol; how you receive the data does not need to match (in terms of packets) how it is sent.
Thus, to send multiple messages on a TCP socket you need to implement some kind of framing mechanism. Any number of framing mechanism are available. A very simple mechanism might be to send a sequence of:
length (4 bytes, little-endian 32-bit integer)
payload ({length} bytes)
length (4 bytes, little-endian 32-bit integer)
payload ({length} bytes)
length (4 bytes, little-endian 32-bit integer)
payload ({length} bytes)
length (4 bytes, little-endian 32-bit integer)
payload ({length} bytes)
And similarly the recipient would buffer 4 bytes, interpret the length, then read that many bytes as a frame; rinse, repeat.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I a very new to C#, so please be aware of that. Currently I am writing a program where a server sends messages to me, that are in format of JSON-strings. How can I read a string from the inputstream of that socket?
You can deserialize JSON-Strings into the proper object with the newtonsoft
You can get a socket as a Stream (for use with most common serializers) by using NetworkStream; however, there is a very important thing to consider: "framing". Since you say "messages" (rather than "message"), it sounds like multiple payloads will be sent on the same socket. TCP just represents an ordered byte-stream; if you're sending one message, this is easy - just pass the NetworkStream to your chosen deserializer, and: job done. But not so easy if you want to transfer multiple payloads over a single connection, since the two ends need to agree on how to know where each payload starts and ends, which is "framing". For text protocols, a common framing mechanism might be to terminate each payload with a sentinel such as a line-end (some combination of CR/LF), or a nul character (byte zero). For binary protocols, this commonly means using a length prefix (in an agreed format) before each message. Unfortunately, in both cases you usually need to buffer the stream until you have a complete payload (noting that you might end up with "a payload and a bit of the next payload" or similar), and use a MemoryStream (or similar) on each buffered frame in turn.
Note that you can also use StreamReader to access a Stream in terms of string via an Encoding; this may be useful if your deserializer takes TextReader or string.
More recently, the "pipelines" API can replace the Stream metaphor, but a: not all serializers support "pipelines" yet, and b: it is quite an advanced topic, so probably not great for "very new to C#" reasons; but if you're interested, I have a multi-part blog series here.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm trying to write a chat application in C# dot net which has two side, a server side and client side.
The server side is unique but the client side could be more than one and data transmission should be encrypted, indeed I would implement encrypted data communication on the both sides.
As a big picture I'd ask what stuffs I need to do?
What I need is, Consider the server side creates a socket and two
clients are going to send data (for ex: client0 gives the address of
client1) then how the server should route the data to the
destination(in this case client1)?
How data encryption should be implemented and which data
cryptographic encryption should be used and how?
I think thought of data encryption might be more complicated task in this time so I'd write the project without implementing data encryption at first ?
Thanks for pretty ideas.
you can do it with TcpClient (client) and with TcpListener (server)
while the communication and data transfer can be used with Threads / async Tasks to get asynchronous and multi threaded functionality.
read this:
http://csharp.net-informations.com/communications/csharp-multi-threaded-server-socket.htm
*EDIT:
it's hard to explain without providing you at least small sample of a project and code, hence I posted the link above which I think can really help you to start off.
as for the routing from 1 client to another,
you can do it in several ways.
one way is to give each client a unique ID or maybe use a unique username (same as most chats out there) and send that unique username. you can create a string on each time you send data with delimiters and parse it when received.
for example:
this will be on the client's side:
string FullMessage = "$#1name" + "yourName" + "$#2name" + "$#1message" + "YourMessage" + "$#2message");
this will be on the server's side:
string MessageRecieved = GetSubstringByString("$#1message", "$#2message", FullMessage);
string Name = GetSubstringByString("$#1name", "$#2name", FullMessage);
public string GetSubstringByString(string startString, string endString, string fullString)
{
return fullString.Substring((fullString.IndexOf(startString) + startString.Length), (fullString.IndexOf(endString) - fullString.IndexOf(startString) - startString.Length));
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Here is my problem :
I'm used to receive data like int, char* & cie... throught a TCP socket in c/c++, but how to manage that in c#? I will communicate with a c++ server, sending me int, bools and char*. I think my bigger problem come from the fact than a char in c# = 2bytes ...
Thanks a lot, and sorry for my poor english :/
Firstly, I really hope you aren't actually sending a char*, as that makes zero sense outside of a given process, and isn't even a well-defined length.
Basically, you need to sit down and write down the encoding rules for all the things you are sending. For example, you might say:
int is 4 bytes little-endian
text is utf-8 encoded with a length-prefix (bytes, not characters) as an int
a bool is a single byte (to avoid boundary issues)
etc for every data type you need
Then figure out how you are going to partition multiple fields of a single message, and how you are going to frame multiple messages in the same socket.
Or perhaps a better option is to choose one of the many pre-existing serialization formats and offload the thinking to that. If you are after efficiency, then "protocol buffers" would be an excellent choice. If you want simplicity maybe JSON.
If you still want to hand-code it, then : start by writing down what exactly it is going to look like on the wire, then implement that.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Sending UDP Packet in C#
I have some data in hex format that I would like to send to a UDP server and then get a response back from the server that contains some data. How would I go about doing this in C#?
I might add that I do not have local access to the server, I would just like to see which response I get by sending this particular data to it.
1- A Chat Application Using Asynchronous UDP sockets
2- Send UDP packet in C#
3- Testing TCP and UDP socket servers using C# and .NET
4- Receive and send data
5- UDP Send and Receive using threads in VB.NET
6- A UDP based Reliable Data Transfer Library
7- msdn microsoft
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
Is there a way to send raw packet Ethernet to other host via C#? In Windows 7 if it makes difference.
Based on suggestion by Saint_pl:
I found probably better solution - similar to SharpPcap. It's Pcap.Net - .NET wrapper for WinPcap. Now I can modify my packets whatever I want.
I have some resources for you that maybe helpful. I don't try that solutions in Windows 7 but maybe it contains some good info to start.
Raw Ethernet Packet Manipulation or mirror on CodeProject
This purpose of this article is to explain how to send a raw Ethernet packet using C# on a Microsoft platform. A raw Ethernet packet is the complete Layer 2 network frame that is sent to the physical wire. Sending a frame like this allows you to manipulate the target and source MAC addresses and the Layer 3 protocol fields.
Also some info on raw sockets (just in case you interesting too):
Client (and Server) Sockets Communication take a look on whole chapter but here key parts:
C# Raw UDP Socket Program Example
C# Raw Socket Ping Program Example part A | part B
All examples
Not sending packets but maybe interesting: A Network Sniffer in C#, SharpPcap - A Packet Capture Framework for .NET
iphelper API has some low level stuff - but probably not quite as low as you want to get
Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint ip = new IPEndPoint(IPAddress.Parse("10.25.184.11"), 4456);
server.Connect(ip);
byte[] sendData = new byte[] { 0, 8, 32, 64 };
server.Send(sendData);
//done. now let's listen for data
byte[] receiveData = new byte[1024];
int receivedDataLength = server.Receive(receiveData);
//if the response is a string message
string stringData = Encoding.ASCII.GetString(receiveData, 0, receivedDataLength);
Console.WriteLine(stringData);