3

Please forgive me for the duplicate question. I know it has been asked here many times but none of the answers worked for me and none of the scenarios are quite the same as mine, and many of the questions have no answers at all.

I have been working on a project for university for a few weeks now but now have been tasked to change the full, working application to a client-server application (the details of the project are irrelevant). I plan on creating a console application that will be hidden and act as the server (will mainly handle SQL queries that will be executed on my database, then create objects based on the results of those queries, serialize them and send them across to the client). The client application is a Windows Forms application.

I am new to using sockets (only started with them 3 days ago but I believe I understand them pretty well - I am able to recreate a client-server chat application from knowledge (and logic) without referring to the code that I was given.)

I have done a lot of research to try fix it but now I have tried everything and I am hoping that someone can help me.

The problem I am having is that my client will not connect to the server. In my "practice" applications I have not had this issue and the client has always managed to connect.

I have tried:

  • Allowing the server (console) and client (forms) applications through the firewall
  • Disabling the firewall completely
  • Made sure the server is listening to the correct port number given on the client-side
  • I even copy-pasted the code (just the sockets part) from my project and created new server and client applications (which worked completely fine, exact same code, put in the exact same place as the original, worked fine in the new "test" applications but not in my project)

After trying these things with no success, I have realised that it is most likely not the firewall that is the issue and there cannot be a problem with the port number I used because the exact same code with the same port numbers worked in my "test" application.

I tried running netstat -anb as per this answer: https://stackoverflow.com/a/9695442/9886482 and I cannot see the port number anywhere.

A few of the other answers I saw on stackoverflow did not help me as most of them are using web services or actual server software which is not relevant or allowed in my case.

I have tried to give as much information as possible, and here is my code. Any help would be greatly appreciated.

Server Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HelperLibrary;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace Farmulator_PRG221_Project
{
class Program
{

    static void Main(string[] args)
    {
        Socket serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
        serverSocket.Bind(new IPEndPoint(IPAddress.Loopback, port: 9000));
        serverSocket.Listen(50);
        Thread accepterThread = new Thread(new ParameterizedThreadStart(Accept));
        accepterThread.Start(serverSocket);
    }

    static void Accept(object obj)
    {
        Socket serverSocket = (Socket)obj;

        while (true)
        {
            Socket clientSocket = serverSocket.Accept();
            Console.WriteLine("New client connected!");

        }

    }

Client code:

clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
clientSocket.Connect(new IPEndPoint(IPAddress.Loopback, port: 9000));
  • Probably TLS issue. Try enabling TLS.. it needs to be set explicitly in .Net 4.6 and higher. – vendettamit Jun 03 '18 at 01:09
  • Probably not related to the actual issue, but you should add a `Console.ReadLine()` or similar to the end of the Server `Main`. Without this, the `accepterThread` will pass out of scope and the GC can collect it, killing the server thread. – Trevor Jun 03 '18 at 02:05
  • Also, try `netstat -aonb` (like you ran previously, but including `o`) and make sure the PID that is listening on port 9000 on the server is your server application Process ID. Might seem a silly thing to check, but it's worth making certain. – Trevor Jun 03 '18 at 02:11
  • I assume the Client and Server are on the same machine? You're using `Loopback` so they would have to be, but you don't state that in the question. – Trevor Jun 03 '18 at 02:12
  • @Trevor Sorry I forgot to mention that yes client and server will be on the same machine. Thanks for your help guys I got it to work, it was a stupid mistake that had nothing to do with the sockets. Thanks for the tip about the `Console.ReadLine()`, I didn't think that would be an issue as the accepterThread was in an endless while loop but I added it in for safety. – Craig van Straaten Jun 04 '18 at 15:09

0 Answers0