Thread: problem with server socket

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    330

    problem with server socket

    I created a C# app which runs a thread accepting socket calls on windows 7 port 8888 (also tried 666) but the connection always times out. Connecting to localhost works fine. Anyone experienced the same issues on windows 7?

  2. #2
    Registered User
    Join Date
    Mar 2009
    Location
    england
    Posts
    209
    Obvious question, but have you port forwarded?

  3. #3
    Registered User
    Join Date
    Jan 2007
    Posts
    330
    Thats not the solution, I have two servers running on the internal network both on that port. But when I only start one and connect with telnet to it having it port fowarded to that pc it still doesnt work

  4. #4
    Registered User
    Join Date
    Mar 2009
    Location
    england
    Posts
    209
    Can you paste some code showing the creation of the socket used for listening?

  5. #5
    Registered User
    Join Date
    Feb 2012
    Posts
    1

    C# socket programming

    check this link: a simple socket program.

    How to C# Socket programming

    gv.

  6. #6
    Registered User
    Join Date
    Jan 2007
    Posts
    330
    Thats the same code where I got it from but adapted a little. Anyway, here is the server socket class.

    Code:
        private class AgendaServer
        {
          private TcpListener mServerSocket;
    
          private AgendaForm mParentForm;
    
          public AgendaServer(TcpListener serverSocket, AgendaForm parentForm)
          {
            mServerSocket = serverSocket;
            mParentForm = parentForm;
          }
    
          public void Server()
          {
            mServerSocket.Start(10);
    
            while (true)
            {                    
              TcpClient client = default(TcpClient);
    
              client = mServerSocket.AcceptTcpClient();
    
              byte[] from = new byte[100000];
    
              NetworkStream nwStream = client.GetStream();
              nwStream.Read(from, 0, from.Length);
    
              UTF8Encoding enc = new UTF8Encoding();
    
              string str = enc.GetString(from);
    
              using (StreamWriter writer = new StreamWriter("agenda.txt"))
              {
                writer.Write(str);
              }
    
              mParentForm.RefreshAgenda();
            }
          }
        }
    I create a Thread which runs AgendaServer.Server and a client sends text to it with this code

    Code:
            Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
    
            IPAddress ip = IPAddress.Parse(ipstring);
    
            s.Connect(ip, 666);
    
            UTF8Encoding enc = new UTF8Encoding();
    
            string text = "";
            using (StreamReader reader = new StreamReader("agenda.txt"))
            {
              string line;
              while ((line = reader.ReadLine()) != null)
              {
                text += line + "\r\n";
              }
            }
    
            s.Send(enc.GetBytes(text));
    
            s.Close();

  7. #7
    Registered User
    Join Date
    Mar 2009
    Location
    england
    Posts
    209
    When creating the TcpListener object are you ensuring you pass IPAddress.Any as one of the arguments?

    Code:
    TcpListener listener = new TcpListener(new IPEndPoint(IPAddress.Any, 666));

  8. #8
    Registered User
    Join Date
    Jan 2007
    Posts
    330
    Aaaah that was it! thanks!

  9. #9
    Registered User
    Join Date
    Mar 2009
    Location
    england
    Posts
    209
    You're welcome.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Raw Socket Client and Server Communication Problem
    By rplumii in forum C Programming
    Replies: 6
    Last Post: 02-10-2011, 07:02 AM
  2. Problem with simple socket client/server program
    By spencer88 in forum C Programming
    Replies: 6
    Last Post: 05-05-2009, 11:05 PM
  3. BSD Socket Server Problem
    By ma_mazmaz in forum C Programming
    Replies: 3
    Last Post: 01-26-2009, 01:03 PM
  4. Socket Server
    By carrotcake1029 in forum Windows Programming
    Replies: 2
    Last Post: 07-21-2008, 11:46 AM
  5. Client/Server Socket Receive Problem
    By mariabair in forum Networking/Device Communication
    Replies: 6
    Last Post: 12-25-2003, 10:01 AM