-2

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

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 > 1) // Test for correct # of args
throw new ArgumentException("Parameters: [<Port>]");

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)) > 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();
 }
 }
 }
 }

From Comment:

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

{ // 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);
}

and program show the error code and displays message like this

10048:Only one usage of each socket address is normally permitted

and program close. What to do?

M4N
  • 94,805
  • 45
  • 217
  • 260
Dinesh Subedi
  • 2,603
  • 1
  • 26
  • 36
  • 3
    _"it doesn't work"_ => What doesn't work? People won't bother to read your code (quite long btw) if you don't provide details on what is wrong, what you have tried... – Otiel Oct 31 '11 at 15:59
  • "It doesn't work" is not a very good problem report. – H H Oct 31 '11 at 16:00
  • 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 { // 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); } and program show the error code and displays message like this 10048:Only one usage of each socket address is normally permitted and program close what to do – Dinesh Subedi Oct 31 '11 at 16:11
  • Once again, the anonymous down voters appear and fail to justify their down votes. Instead of down voting the poster why not try helping him, did you think for one second that maybe English isn't his first language?? The purpose of this forum to to assist others not to denigrate and ridicule them. Grow up people! – Mark Kram Oct 31 '11 at 16:21
  • 1
    Iam new so I did mistake.But I know "who does nothing make no mistake who make no mistake learns nothing". – Dinesh Subedi Oct 31 '11 at 16:58
  • 1
    @Dinesh - No worries, unfortunately there are some idiots here who take pride in making others look stupid. Fortunately there are a lot of brilliant people here that will go way out of there way to help you out. Don't let the idiots get you down and keep plugging away. – Mark Kram Oct 31 '11 at 22:08

2 Answers2

2

The port you specify in new TcpListener(IPAddress.Any, servPort); seems to be in use, which is not permitted (only one program can listen on a specific port).

This can be either because you have multiple instances of your server program running, or because it is used by another program. For example, port 80 is usually in use if you have a web server running on your machine.

Try to chose another port number (e.g. 10000). I would avoid lower port numbers (especially below 1024) since these are used by well known programs (web servers, mail servers, etc. - see this wikipedia page for more information)

M4N
  • 94,805
  • 45
  • 217
  • 260
  • @Dinesh: Don't forget to upvote helpful answers and to accept the one providing you the solution. – M4N Oct 31 '11 at 16:50
1

If you used Visual Studio 2010 to compile and run this (via Debug), you should set up command line arguments as your code requires them.

If you already did run your program with command line arguments (either through console or by setting them up in VS), then please give a more detailed explanation of why it doesn't work.

To set up command line arguments, right click on your project in Solution Explorer -> Properties, then in Debug tab, type a value representing a port in the Command Line Arguments field. No spaces, only one value.

neeKo
  • 4,280
  • 23
  • 31