Click here to Skip to main content
15,900,973 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
C#
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();
 }
 }
 }
 }</port>
Posted
Comments
hilmisu 31-Oct-11 11:30am    
what is that at the end of last line? "</port>"
BobJanova 31-Oct-11 12:08pm    
That's because he forgot to escape the chevrons in 'Parameters: <port>'.
smokindinesh 31-Oct-11 11:53am    
nothin just I did mistake while pasting code you can ignore it

From everything I can tell the code does work. The code is actively listening for client connections and will appear to do nothing until it finds one. Maybe you're not running a client to connect to it?

The TcpEchoServer is meant to be run with a client that is sending it messages. Those messages get echoed back to the client once received.

I don't have that particular book, but look around and see if there is a TcpEchoClient (or something similar) somewhere else in the chapter. You will need to run that along side the server (using the same port) to see either of them work to completion.

It is worth noting that if you don't run from the command line and provide parameters there that the default port is going to be 7. You'll need to use that same port in the client. I'm going to guess that the echo client will use that same port by default, so it is probably a non-issue, but keep it in mind.
 
Share this answer
 
v3
Comments
smokindinesh 31-Oct-11 11:52am    
last lin </port> I did mistake while pasting code it is nothing.According to your answer I have program for client also .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 <protocol/network address/port> is normally permitted
and program close what to do
Jon McClure 31-Oct-11 12:08pm    
That error means that there is another program making use of that port or that another program has made use of that port very recently. This could be a remnant from a previously un-closed TCPListener or it might be some other program on your computer. Try changing the port to something like 49780 (for both the TcpEchoServer and the TcpEchoClient) and see if that works. Restarting your computer may also clear up whatever is keeping that port open.
smokindinesh 31-Oct-11 12:50pm    
Thanks really I got the solution
I suspect you don't fully understand what that code does. It creates a server socket and listens on the specified port (defaults to port 7). Now you need a client to connect to that port, until then nothing will happen.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900