[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework

Bi-directional Socket Listener/Receiver

Doug

3/2/2011 12:09:00 AM

Hey,
I want to create a very quick and dirty bi-directional socket listener
and receiver and have been trying to do it with this code and cannot
get it to work...could someone show me what I'm doing wrong?

private void SendFile(FileInfo file)
{
IPAddress address = IPAddress.Parse(txtIPAddress.Text);

Socket server = server = new
Socket(AddressFamily.InterNetwork, SocketType.Stream,
ProtocolType.Tcp);
server.Bind(new IPEndPoint(address,
int.Parse(txtPort.Text)));
server.Listen(5);

using (FileStream streamFile = file.OpenRead())
{
byte[] fileBuffer = new byte[4096];
long byteTotal = file.Length;
int byteRead = 0;
int byteSum = 0;

while (byteSum < byteTotal)
{
byteRead = streamFile.Read(fileBuffer, 0,
fileBuffer.Length);
byteSum += byteRead;
}

server.Send(fileBuffer);

byte[] messageBuffer = new byte[1024];

Socket client = null;
string message = string.Empty;


while (true)
{
client = null;
message = string.Empty;

try
{
client = server.Accept();

ASCIIEncoding ascii = new ASCIIEncoding();

char[] messageCharacters = null;
int numberBytes;
int totalBytes = 0;

while ((numberBytes =
client.Receive(messageBuffer, 0, messageBuffer.Length,
SocketFlags.None)) > 0)
{
messageCharacters =
ascii.GetChars(messageBuffer, 0, numberBytes);

for (int index = 0; index <=
(messageCharacters.Length - 1); index++)
{
message += messageCharacters[index];
}

Array.Clear(messageBuffer, 0,
messageBuffer.Length);
totalBytes += numberBytes;
}


client.Close();

string fileName = Path.Combine(@"C:\Doug\Write
\Socket\", Path.GetRandomFileName());

using (StreamWriter writer = new
StreamWriter(fileName))
{
writer.Write(message);
}
}
catch (Exception ex)
{
client.Close();
throw;
}
}
}

}
1 Answer

Peter Duniho

3/3/2011 4:26:00 AM

0

On 3/1/11 3:09 PM, Doug wrote:
> Hey,
> I want to create a very quick and dirty bi-directional socket listener
> and receiver and have been trying to do it with this code and cannot
> get it to work...could someone show me what I'm doing wrong?

Where to begin? :)

There's really too much wrong with the code to go through bit by bit and
explain each problem. You really should just start by reading the MSDN
documentation, especially the pages relevant to the Socket class.
Follow the code examples there (they aren't perfect, but they are a
better starting point than the code you posted :) ).

But here are some highlights:

• You can't send on a listening socket.
• You must check the byte counts for all return values for send and
receive, and accommodate return values that show fewer bytes have been
processed than what you requested.
• Assuming your server is supposed to be sending the data, then you
cannot receive the data from the accepted socket. You have to _send_ the
data to that socket.
• You cannot run both the send and receive logic on the same thread;
doing so could easily result in an i/o form of deadlock as one i/o call
blocks waiting for some condition that will be satisfied only by the
other i/o call.

Also, not a bug per se, but there's really no reason to do all that work
calling Encoding.GetChars() and then concatenating each character
individually. Just call GetString(), and append the returned string as
a single unit. And when appending the string, don't just concatenate
strings; use StringBuilder and call the Append() method for each new string.

Pete