Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello all,

i am able to connect server through telnet and receive a resonse through telnet but if i do same through my code i am not able to get that. i am very sure that my code is right as i already tested it by creating client and server on my local.

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Configuration;

namespace ClientSocket
{
    public partial class Client : Form
    {
        System.Net.Sockets.TcpClient clientSocket = new System.Net.Sockets.TcpClient();
        NetworkStream serverStream; 

        public Client()
        {
            InitializeComponent();
        }

        private void Client_Load(object sender, EventArgs e)
        {
            msg("Client Started");
            //clientSocket.Connect(ConfigurationManager.AppSettings["ip"].ToString(), Convert.ToInt16(ConfigurationManager.AppSettings["port"]));
            clientSocket.Connect("172.29.29.6", 7506);
            label1.Text = "Client Socket Program - Server Connected ...";
        }

        public void msg(string mesg)
        {
            textBox1.Text = textBox1.Text + Environment.NewLine + " >> " + mesg;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            NetworkStream serverStream = clientSocket.GetStream();
            byte[] outStream = System.Text.Encoding.ASCII.GetBytes(txtMessage.Text);
            serverStream.Write(outStream, 0, outStream.Length);
            serverStream.Flush();

            byte[] inStream = new byte[1000768];
            serverStream.Read(inStream, 0, (int)clientSocket.ReceiveBufferSize);
            //serverStream.Read(inStream, 0, 1000768);
            string returndata = System.Text.Encoding.ASCII.GetString(inStream);
            msg("Data from Server : " + returndata);

        } 


    }
}
Posted
Comments
Rob Philpott 19-Sep-13 5:02am    
What error are you given?
Richard MacCutchan 20-Sep-13 4:01am    
Your main problem is that you do not check the results of any of your operations, so you have no idea whether anything works.
sunil mali 20-Sep-13 5:47am    
it is hanging on read
serverStream.Read(inStream, 0, (int)clientSocket.ReceiveBufferSize);

after some time it returns back but its empty...

your ip's must valid in yournetwork

and other configuration of send packet must be set

because windows automatically set other items.
 
Share this answer
 
Comments
sunil mali 19-Sep-13 7:48am    
on below line my application is getting hanged.

serverStream.Read(inStream, 0, (int)clientSocket.ReceiveBufferSize);
I think the most likely thing is that the connect method is expecting a hostname, not an IP address.

Try this:

IPAddress address = IPAddress.Parse("172.29.29.6");
clientSocket.Connect(address, 7506);
 
Share this answer
 
Comments
sunil mali 19-Sep-13 7:21am    
I tried still same issue..
in command prompt if i type
telnet ip port
message
ctrl+s
it gives me response.. here ctrl+s acts as terminator of message ...
how do i adapt this in my code...
because my code keeps on waiting for response...
Rob Philpott 19-Sep-13 7:54am    
Are you getting nothing back at all?
sunil mali 20-Sep-13 2:00am    
it is hanging on read
serverStream.Read(inStream, 0, (int)clientSocket.ReceiveBufferSize);

after some time it returns back but its empty...
Rob Philpott 19-Sep-13 7:53am    
Ok - forget that. I would expect this to work. Something else to try - use UTF8 encoding rather than ASCII, but if anything I think you have it the right way round...
sunil mali 20-Sep-13 1:59am    
i tried utf8 also...
the line where it reads from serverstream... it gets hanged there...

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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