Click here to Skip to main content
15,904,153 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is the Sever Code, am receiving the data from the client but every time am getting one special symbol before the data.

For Example :
If client sends 1 then
In The Server i will get outout: @1 any symbols
like this am getting special symbols and values.
C#
try {
    int Port = 8088;
    TcpListener server = new TcpListener(IPAddress.Any, Port);
    server.Start()
    byte[] bytes = new byte[1024];
    string data;
    while (true)
      {
        Console.Write("Waiting for Client connection... ");
        TcpClient client = server.AcceptTcpClient();
        Console.WriteLine("\nClient Connected....!");
        NetworkStream stream = client.GetStream();
        int i;
        i = stream.Read(bytes, 0, bytes.Length);
        while (i != 0)
           {
              i = stream.Read(bytes, 0, bytes.Length);
              data = System.Text.Encoding.ASCII.GetString(bytes, 0,i);
              Console.WriteLine(string.Format("Received:{0}",data));
             }
       client.Close();
     }
 }
catch (SocketException e)
 {
    Console.WriteLine("SocketException: {0}", e);
 }
Posted
Updated 14-Apr-15 3:31am
v5

You can't directly apply AcceptTcpClient as if it returns a client instance. It would also be complete useless because you could only connect from the software that also runs as a server.

There are several examples here on codeproject you can check out:
A TCP/IP Server written in C#[^]

More advanced:
A Complete TCP Server/Client Communication and RMI Framework in C# .NET - Implementation[^]

Simple example:
http://tech.pro/tutorial/704/csharp-tutorial-simple-threaded-tcp-server[^]

Good luck!
 
Share this answer
 
v2
int Port = 8088;
 TcpListener server = new TcpListener(IPAddress.Any, Port);
 server.Start(); 
 byte[] bytes = new byte[1024];
 string data;
 Console.Write("Waiting for Client connection ");
  TcpClient client = server.AcceptTcpClient();
  Console.WriteLine("\n Client Connected");
   NetworkStream stream = client.GetStream();
   int i;
i = stream.Read(bytes, 0, bytes.Length);
data = Encoding.UTF8.GetString(bytes, 0, i);
 data=data.Substring(2, data.Length-2);
 Console.WriteLine(string.Format("Data Received :{0}", data));
  client.Close()
 
Share this answer
 
v2

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