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

Solution 1

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.
  Permalink  
v3
Comments
smokindinesh at 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 at 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 at 31-Oct-11 12:50pm
   
Thanks really I got the solution
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 304
1 Maciej Los 285
2 Sergey Alexandrovich Kryukov 255
3 Shweta N Mishra 216
4 PIEBALDconsult 174
0 OriginalGriff 7,660
1 Sergey Alexandrovich Kryukov 7,072
2 DamithSL 5,604
3 Manas Bhardwaj 4,986
4 Maciej Los 4,760


Advertise | Privacy | Mobile
Web03 | 2.8.1411023.1 | Last Updated 31 Oct 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100