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));