Note that there are some explanatory texts on larger screens.

plurals
  1. POTCP Server in C#
    primarykey
    data
    text
    <p>Iam just a beginner student socket programming.I tried a simple code from "TCP/IP Sockets in C# Practical Guide for Programmers" pdf book but it doesn't work.I compiled it in visual studio 2010.Please help me what is wrong and here is the complete code</p> <pre><code>using System; // For Console, Int32, ArgumentException, Environment using System.Net; // For IPAddress using System.Net.Sockets; // For TcpListener, TcpClient class TcpEchoServer { private const int BUFSIZE = 32; // Size of receive buffer static void Main(string[] args) { if (args.Length &gt; 1) // Test for correct # of args throw new ArgumentException("Parameters: [&lt;Port&gt;]"); int servPort = (args.Length == 1) ? Int32.Parse(args[0]): 7; TcpListener listener = null; try { // Create a TCPListener to accept client connections listener = new TcpListener(IPAddress.Any, servPort); listener.Start(); } catch (SocketException se) { Console.WriteLine(se.ErrorCode + ": " + se.Message); Environment.Exit(se.ErrorCode); } byte[] rcvBuffer = new byte[BUFSIZE]; // Receive buffer int bytesRcvd; // Received byte count for (;;) { // Run forever, accepting and servicing connections TcpClient client = null; NetworkStream netStream = null; try { client = listener.AcceptTcpClient(); // Get client connection netStream = client.GetStream(); Console.Write("Handling client - "); // Receive until client closes connection, indicated by 0 return value int totalBytesEchoed = 0; while ((bytesRcvd = netStream.Read(rcvBuffer, 0, rcvBuffer.Length)) &gt; 0) { netStream.Write(rcvBuffer, 0, bytesRcvd); totalBytesEchoed += bytesRcvd; } Console.WriteLine("echoed {0} bytes.", totalBytesEchoed); // Close the stream and socket. We are done with this client! netStream.Close(); client.Close(); } catch (Exception e) { Console.WriteLine(e.Message); netStream.Close(); } } } } </code></pre> <p><strong>From Comment:</strong></p> <p>I have program for client also to connect with this server program. Actual problem is this server program does not run. At line 16-22 there is the code try</p> <pre><code>{ // Create a TCPListener to accept client connections listener = new TcpListener(IPAddress.Any, servPort); listener.Start(); } catch (SocketException se) { Console.WriteLine(se.ErrorCode + ": " + se.Message); Environment.Exit(se.ErrorCode); } </code></pre> <p>and program show the error code and displays message like this</p> <blockquote> <p>10048:Only one usage of each socket address is normally permitted</p> </blockquote> <p>and program close. What to do?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload