Click here to Skip to main content
11,642,658 members (69,403 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: C#
i wrote a synchron tcp client socket which can just sending data,no receive.
the problem is socket just ont time send data and the next time i have this error:

"Once the socket has been disconnected, you can only reconnect again asynchronously, and only to a different EndPoint. BeginConnect must be called on a thread that won't exit until the operation has been completed."

i know the problem is because of while condition.but why?

<%@ Page Language="C#" AutoEventWireup="true" %>
<%@ Import Namespace="System"%>
<%@ Import Namespace="System.Net"%>
<%@ Import Namespace="System.Net.Sockets"%>
<%@ Import Namespace="System.Text"%>
<%@ Import Namespace="System.IO"%>
<!DOCTYPE html>
<html xmlns="">
<head id="Head1" runat="server">
    <script language="c#" runat="server"> 
        private static int port = 9050;
        private static string serverip = "";
        private static int timeoutHour = 0;
        private static int timeoutMin = 3;
        private static int timeoutScnd = 10;
        private static int timeoutMilScnd = 0;
        private static int BufferSize = 1024;
        private static byte[] data = new byte[BufferSize];
        private static string msg = "Hello";
        protected void Page_Load(object sender, EventArgs e)
        private void Run()
            IPEndPoint ipep = new IPEndPoint(IPAddress.Parse(serverip), port);
            Socket serverSckt = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                       DateTime TimeAtStart = new DateTime();
                        TimeAtStart = DateTime.Now;
                        TimeSpan TimeOut = new TimeSpan(timeoutHour, timeoutMin, timeoutScnd,timeoutMilScnd);

                        while (TimeOut > (DateTime.Now - TimeAtStart))
                            Connecting(ipep, serverSckt);
                            TcpSend(serverSckt, msg);
        private void Connecting(IPEndPoint ipep, Socket serverSckt)
                serverSckt.Connect(ipep); //error is here
            catch (SocketException ex)
                Response.Write("Unable to Connect to server");
        private void TcpSend(Socket server, string message)
        private static void ConnectionClosed(Socket sockt)
    <form id="form1" runat="server">
Posted 6-May-13 19:06pm
patel_vijay at 7-May-13 2:04am
You should not keep connection socket again and again.
Put Connecting(ipep, serverSckt); out side of while loop.

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

  Print Answers RSS
0 DamithSL 254
1 Sergey Alexandrovich Kryukov 204
2 OriginalGriff 160
3 Afzaal Ahmad Zeeshan 129
4 Mika Wendelius 85
0 Mika Wendelius 485
1 DamithSL 457
2 OriginalGriff 365
3 Afzaal Ahmad Zeeshan 258
4 Sergey Alexandrovich Kryukov 254

Advertise | Privacy | Mobile
Web04 | 2.8.150731.1 | Last Updated 7 May 2013
Copyright © CodeProject, 1999-2015
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