So I’m trying to get a console app running where the server and client can message each other i had it working before but it was with blocking or synchronous lines like send or receive and i wanted to switch over to unblocking because either the client or server had to wait to receive a message to send one and it wasn’t practical so i started with BeginAccept to get a feel of how they work but my beginAccept isn’t calling back to the static void that has the endAccept for a proper connection
Any and all help is greatly appreciated
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
public class Server
{
public static void Main()
{
//Set ipv4 address and socket to variables
IPAddress ServerAddress = IPAddress.Parse("127.0.0.1");
int ServerPort = 1234;
bool connected = false;
//Setup the new socket with the parameters of addressFamily.interNetwork (tells it to use a ipv4 address)
//socketType.stream and protocolType.tcp (tells it to have a connection tye of tcp)
Socket ServerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
ServerSocket.Bind(new IPEndPoint(ServerAddress, ServerPort));
//telling the socket to start listening and to only let 5 connections at one time
ServerSocket.Listen(5);
Console.WriteLine("Server is listening for connections");
while (!connected)
{
ServerSocket.BeginAccept(new AsyncCallback(AcceptCallback), ServerSocket);
if (ServerSocket.Connected == true)
{
connected = true;
Console.WriteLine("Client connected!");
}
}
while (connected == true)
{
if (ServerSocket.Connected == true)
{
connected = true;
}
else
{
connected = false;
}
//setup a array called temp to store the clients data, set what you recieve from clientSocket to be put into an int called ClientBytes
//convert the bytes from clientbytes to string to read it
byte[] temp = new byte[1024];
int ClientBytes = ServerSocket.Receive(temp);
if (ClientBytes <= 0 || temp == null)
{
return;
}
else
{
string ClientMessage = Encoding.ASCII.GetString(temp, 0, ClientBytes);
Console.WriteLine("Recieved data from client: " + ClientMessage);
}
//Convert the message to bytes, store it in a array then send it down the client socket
string ServerMessage = Console.ReadLine();
if (ServerMessage == null || ServerMessage.Length == 0)
{
return;
}
else
{
byte[] temp2 = Encoding.ASCII.GetBytes(ServerMessage);
ServerSocket.Send(temp2);
}
/*ClientSocket.Shutdown(SocketShutdown.Both);
ClientSocket.Close();*/
}
if (Console.KeyAvailable && Console.ReadKey(true).Key == ConsoleKey.F10)
{
ServerSocket.Close();
}
}
private static void AcceptCallback(IAsyncResult ar)
{
Socket ServerSocket = (Socket)ar.AsyncState;
Socket Handler = ServerSocket.EndAccept(ar);
}
}
I expected it to just connect but it didn’t. I haven’t tried much to be honest because i thought i just got a good understanding of it but i guess not
Doblob83 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.