Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0 WinForm .NET4.5
Hi,
 
I am getting a connection for my socket programming code...
This is just a part of an application, which I am developing...
 
It takes some data from use and stores into database, but this is not executing... Frown | :(
client side ip address- 172.21.5.1; port = 5555;
client side ip address- 172.21.5.2; port = 5555;
 
client side code...
endpint = new IPEndPoint(IPAddress.Parse(clientip), port);
 
            serverendpoint = new IPEndPoint(IPAddress.Parse(serverip), serverport);
 
            ASCIIEncoding asc = new ASCIIEncoding();
 
            Client = new TcpClient();
            clientsocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
 
            try
            {
                Client.Connect(serverendpoint);
 
                NetworkStream servernetstrm = Client.GetStream();
 
                servernetstrm.Write(buffer_name, 0, buffer_name.Length);
                servernetstrm.Write(buffer_desc, 0, buffer_desc.Length);
                servernetstrm.Write(buffer_strtdate, 0, buffer_strtdate.Length);
                servernetstrm.Write(buffer_endate, 0, buffer_endate.Length);
             
                servernetstrm.Flush();
 
            }
 
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
 
            Client.Close()
}
 
server side code...
clientendpoint = new IPEndPoint(IPAddress.Parse(clientip),port);
 
            try
            {
                client = default(TcpClient);
                server = new TcpListener(clientendpoint);
                               
                serversocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                serverstream = new NetworkStream(serversocket);
 
                serverstream = client.GetStream();
 
                ASCIIEncoding asc = new ASCIIEncoding();
 
                serversocket.Bind(clientendpoint);
                serversocket.Listen(1000);
                client = server.AcceptTcpClient();
 
                server.Start();
 
                while (true)
                {                 
                    //worker wrk = new worker();
                    //wrk.startClient(client);

                    //get client data...
                    taskname = asc.GetString(buffer_name);
                    task_desc = asc.GetString(buffer_desc);
                    task_start_date = asc.GetString(buffer_strtdate);
                    task_endate = asc.GetString(buffer_endate);
 
                    SqlCommand cmd = new SqlCommand("insert into accounts ([task name],[task descp],[task start date],[task end date]) Values('"+taskname+"','"+task_desc+"','"+task_start_date+"','"+task_endate+"')", empdata);
                    
                    empdata.Open();
                    int res =  cmd.ExecuteNonQuery();
                    empdata.Close();
 
                    if (res == 1)
                    {
                        MessageBox.Show("New Task created Successfully");
                    }
 
                    server.Stop();
                }              
            }
 
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
 

I am getting this error
 
"A connection attempt failed because the connection party did not responded over a period of time or establieshed connection failed because......."
THE CLIENT AND SERVER ARE RUNNING ON SAME SYSTEM....
 
Could anyone please help me with this issue ?
 
Thanks in advance... Smile | :)
Posted 14-Mar-13 2:11am
Edited 14-Mar-13 2:32am
(no name)114K
v2
Comments
ryanb31 at 14-Mar-13 7:34am
   
I would put some breakpoints in on both sides and see what is happening.
Member 7849477 at 14-Mar-13 7:41am
   
thanks for the reply....
but i had used breakpoint on clientside code...
 
at Client.Connect(serverendpoint);
it shows Connected:false....
 
ryanb31 at 14-Mar-13 7:50am
   
Sounds like it is just an issue with the server not being started or the server is not listening on the port that the client is trying to connect to or does the server need to capture an event when a client needs to connect?
Member 7849477 at 14-Mar-13 7:52am
   
so, what should i do to resolve this error ... client/server have same port number.....
 
Please do guide me... :)
ryanb31 at 14-Mar-13 8:02am
   
I haven't worked with this in a long time. There is an article on this site that should help: http://www.codeproject.com/Articles/1415/Introduction-to-TCP-client-server-in-C

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



Advertise | Privacy | Mobile
Web02 | 2.8.1411023.1 | Last Updated 14 Mar 2013
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