Click here to Skip to main content
15,891,473 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am receiving data from the IP "59.92.111.69" which is sent by an GPRS device, but i am not getting correct data. The data which i am receiving is only question mark("�"). Please help me to find the solution.
Here is my coding.

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.WebSockets;
using System.Net.Sockets;
using System.Net;
using System.Text;
using System.Threading.Tasks;



namespace TCP_IP_Client
{
    public partial class client : System.Web.UI.Page
    {
        System.Net.Sockets.TcpClient clientsocket = new System.Net.Sockets.TcpClient();
        
        protected void Page_Load(object sender, EventArgs e)
        {
            clientsocket.Connect("59.92.111.69", 8080);
            Label1.Text = "Server connected";
            
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            NetworkStream serverStream = clientsocket.GetStream();
           // byte[] outStream = System.Text.Encoding.ASCII.GetBytes(TextBox2.Text + "$");
            //serverStream.Write(outStream, 0, outStream.Length);
            //serverStream.Flush();
            byte[] inStream = new byte[10025];
            serverStream.Read(inStream, 0, (int)clientsocket.ReceiveBufferSize);
            Response.Write(clientsocket.ReceiveBufferSize);
            string returndata = System.Text.Encoding.UTF7.GetString(inStream);
            //string returndata = System.Text.Encoding.UTF32.GetString(inStream);
            //s.Length.ToString();
            msg(returndata);
            TextBox2.Text = "";
            TextBox2.Focus();
            
            
        }
        public void msg(string mesg)
        {
            
            TextBox1.Text = Environment.NewLine + " and " + mesg;

        } 
    }
}


What I have tried:

I have tried the encoding formats ASCII, UTF32, UTF7, UTF8 and default encoding methods.
Posted
Updated 18-Apr-16 3:22am
v2

1 solution

What makes you think it's an encoding problem?
A quick check here says there is no real data being returned: the data buffer is full of nulls.

So check your IP address, check the remote end, check your data.
And...watch your sizes: the default receive buffer size is considerably larger than the buffer you have allocated!
 
Share this answer
 
Comments
S.Soundar 18-Apr-16 9:23am    
I think its encoding problem.

Today i tried this by giving input manually in asp.net.

Please find the codes below. it still contains the question mark in the result.

Client side coding:

NetworkStream serverStream = clientSocket.GetStream();
TextBox1.Text = "Client Socket Program - Server Connected ...";
byte[] inStream = new byte[90025];
serverStream.Read(inStream, 0, (int)clientSocket.ReceiveBufferSize);
//TextBox2.Text = inStream.ToString();
string returndata = System.Text.ASCIIEncoding.ASCII.GetString(inStream);
msg(returndata);
serverStream.Flush();


Server side coding:

TextBox1.Text="Server Started";
clientsocket = serversocket.AcceptTcpClient();
TextBox1.Text = TextBox1.Text + " \n Accept Connection from Client";
//Console.WriteLine("Accept connection from client");
reqcount = 0;

//sample
reqcount = reqcount + 1;
NetworkStream netstream = clientsocket.GetStream();
TextBox2.Text = "Hi! this is from the server.";
string serverresponse = TextBox2.Text;
Byte[] sendbytes = System.Text.Encoding.ASCII.GetBytes(serverresponse);
netstream.Write(sendbytes, 0, sendbytes.Length);

netstream.Flush();

And i am getting output as

"Server:Hi! this is from the server.�����������������������������������������������������������������������������������������"

this question mark is still appearing in the result. please help me i don't know what is wrong. help me to correct this. Thank you.
OriginalGriff 18-Apr-16 9:37am    
Yes. Because you are sending a string of about 20 or 30 bytes and printing a buffer that is 90,025 bytes long. What did you expect the "unused" bytes to contain?

It's not encoding. Read what I originally said, and try that first.
Richard Deeming 18-Apr-16 11:31am    
As Griff said, you need to check the number of bytes returned:

int bytesRead = serverStream.Read(inStream, 0, (int)clientsocket.ReceiveBufferSize);
string returndata = System.Text.Encoding.ASCII.GetString(inStream, 0, bytesRead);
S.Soundar 19-Apr-16 0:10am    
Thank u so much OriginalGriff and Richard. Now i realize the problem:-).
OriginalGriff 19-Apr-16 6:10am    
Check your code and make sure you Closed and Disposed the Socket correctly. If that doesn't help, you will have to open this as a new question, since it is unrelated to the existing one.

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