C
C#9mo ago
zoon_

✅ Help with sockets c#

how would i go by showing a message to every client connected to a server ?
96 Replies
gwon8266
gwon82669mo ago
Ehmmm.. Check out SignalR library
Jimmacle
Jimmacle9mo ago
loop over your connections and send each of them a message? kind of an abstract question that depends on how your server is implemented
zoon_
zoon_OP9mo ago
im not using any library just sockets okay but how do i loop over them when i dont have them stored ? is there a way to store them ? ill show you my server class
Jimmacle
Jimmacle9mo ago
you have to be storing them somewhere already
zoon_
zoon_OP9mo ago
my main issue is how do i get reference from my textbox in the client form
Jimmacle
Jimmacle9mo ago
you might need to add a list of them so you can access all the active connections easily
zoon_
zoon_OP9mo ago
im using winform btw
Jimmacle
Jimmacle9mo ago
unfortunate (not for this problem, just in general)
zoon_
zoon_OP9mo ago
yeah even if i access that.. my main issue is how to get reference to a chat text box in another form
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using ChatApplication.Client;
using ChatApplication.Classes;

namespace ChatApplication.Server
{
internal class Server
{
public string _address = "127.0.0.1";
public int _port = 8888;
public static Socket serverSocket;
Dictionary<Account, string> clients;

public Server(IPEndPoint EndPoint)
{

serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
serverSocket.Bind(EndPoint);
Thread serverThread = new Thread(() => StartListening());
serverThread.Start();
}

public void StartListening()
{
MessageBox.Show("Started listening!");
int clientNo = 1;
while(true)
{
try
{
serverSocket.Listen(0);
Socket clientSocket = serverSocket.Accept();
Thread clientThread = new Thread(() => ClientConnection(clientSocket, clientNo));
clientThread.Start();
clientNo++;
}
catch (Exception ex) {
MessageBox.Show(ex.Message);
};
}
}

private void ClientConnection(Socket clientSocket, int clientNo)
{
byte[] buffer = new byte[clientSocket.SendBufferSize];
int bytesRead = 0;
do
{
bytesRead = clientSocket.Receive(buffer);
MessageBox.Show($"Client({clientNo}) Has sent a message: {System.Text.Encoding.UTF8.GetString(buffer)}");
} while (bytesRead > 0);
}

}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using ChatApplication.Client;
using ChatApplication.Classes;

namespace ChatApplication.Server
{
internal class Server
{
public string _address = "127.0.0.1";
public int _port = 8888;
public static Socket serverSocket;
Dictionary<Account, string> clients;

public Server(IPEndPoint EndPoint)
{

serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
serverSocket.Bind(EndPoint);
Thread serverThread = new Thread(() => StartListening());
serverThread.Start();
}

public void StartListening()
{
MessageBox.Show("Started listening!");
int clientNo = 1;
while(true)
{
try
{
serverSocket.Listen(0);
Socket clientSocket = serverSocket.Accept();
Thread clientThread = new Thread(() => ClientConnection(clientSocket, clientNo));
clientThread.Start();
clientNo++;
}
catch (Exception ex) {
MessageBox.Show(ex.Message);
};
}
}

private void ClientConnection(Socket clientSocket, int clientNo)
{
byte[] buffer = new byte[clientSocket.SendBufferSize];
int bytesRead = 0;
do
{
bytesRead = clientSocket.Receive(buffer);
MessageBox.Show($"Client({clientNo}) Has sent a message: {System.Text.Encoding.UTF8.GetString(buffer)}");
} while (bytesRead > 0);
}

}
}
here is my server code
using ChatApplication.Classes;
using System.Net;
using System.Net.Sockets;

namespace ChatApplication.Client
{
internal class Client
{
public Account account;
Socket clientSocket;
public Client(IPEndPoint endPoint)
{
clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
clientSocket.Connect(endPoint);
clientSocket.SendBufferSize = 1024;
}
public void SendMessage(string message)
{
clientSocket.Send(System.Text.Encoding.UTF8.GetBytes(message));
}

//public void CreateAccount(string username, string password)
//{
// account = new Account(username, password);
//}
public void CloseConnection()
{
clientSocket.Close();
}
}
}
using ChatApplication.Classes;
using System.Net;
using System.Net.Sockets;

namespace ChatApplication.Client
{
internal class Client
{
public Account account;
Socket clientSocket;
public Client(IPEndPoint endPoint)
{
clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
clientSocket.Connect(endPoint);
clientSocket.SendBufferSize = 1024;
}
public void SendMessage(string message)
{
clientSocket.Send(System.Text.Encoding.UTF8.GetBytes(message));
}

//public void CreateAccount(string username, string password)
//{
// account = new Account(username, password);
//}
public void CloseConnection()
{
clientSocket.Close();
}
}
}
client code (this is not final i just want to figure out this chat box thing)
zoon_
zoon_OP9mo ago
using System.Net;
using System.Net.Sockets;
using ChatApplication.Classes;

namespace ChatApplication.Client
{
public partial class ClientHomePage : Form
{
Client client;
public ClientHomePage()
{
InitializeComponent();
}
private void BTN_Connect_Click(object sender, EventArgs e)
{
// Connect to the server
client = new Client(new IPEndPoint(IPAddress.Parse(TB_HostAddress.Text), int.TryParse(TB_PortNumber.Text, out int port) ? port : 8888));
//client.CreateAccount("Mazen", "123123");
}

private void BTN_SendMessage_Click(object sender, EventArgs e)
{
// Send message
client.SendMessage(TB_MessageBox.Text);
}

private void TB_MessageBox_TextChanged(object sender, EventArgs e)
{

}


private void TB_ChatBox_TextChanged(object sender, EventArgs e)
{

}
private void ClientHomePage_FormClosed(object sender, FormClosedEventArgs e)
{
client.SendMessage("Goodbye..");
client.CloseConnection();
}
}
}
using System.Net;
using System.Net.Sockets;
using ChatApplication.Classes;

namespace ChatApplication.Client
{
public partial class ClientHomePage : Form
{
Client client;
public ClientHomePage()
{
InitializeComponent();
}
private void BTN_Connect_Click(object sender, EventArgs e)
{
// Connect to the server
client = new Client(new IPEndPoint(IPAddress.Parse(TB_HostAddress.Text), int.TryParse(TB_PortNumber.Text, out int port) ? port : 8888));
//client.CreateAccount("Mazen", "123123");
}

private void BTN_SendMessage_Click(object sender, EventArgs e)
{
// Send message
client.SendMessage(TB_MessageBox.Text);
}

private void TB_MessageBox_TextChanged(object sender, EventArgs e)
{

}


private void TB_ChatBox_TextChanged(object sender, EventArgs e)
{

}
private void ClientHomePage_FormClosed(object sender, FormClosedEventArgs e)
{
client.SendMessage("Goodbye..");
client.CloseConnection();
}
}
}
client home page which also looks like this
No description
zoon_
zoon_OP9mo ago
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ChatApplication.Server
{
public partial class ServerHomePage : Form
{
Server? server;
public ServerHomePage()
{
InitializeComponent();
}




private void ServerHomePage_FormClosed(object sender, FormClosedEventArgs e)
{
// Close the server
//Server.serverSocket.Disconnect(true);
}

private void BTN_ServerStart_Click(object sender, EventArgs e)
{
// Start the server
try
{
IPEndPoint ipEnd = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8888);
server = new Server(ipEnd);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ChatApplication.Server
{
public partial class ServerHomePage : Form
{
Server? server;
public ServerHomePage()
{
InitializeComponent();
}




private void ServerHomePage_FormClosed(object sender, FormClosedEventArgs e)
{
// Close the server
//Server.serverSocket.Disconnect(true);
}

private void BTN_ServerStart_Click(object sender, EventArgs e)
{
// Start the server
try
{
IPEndPoint ipEnd = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8888);
server = new Server(ipEnd);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
server home page
zoon_
zoon_OP9mo ago
just a start that starts the server
No description
zoon_
zoon_OP9mo ago
what i have rn is whenever a client sends a message it shows a pop up with client(2) has sent a message: <message here> the client number increases with the number of client threads soon i want to add a client name etc but now i just wanna figure out a way to show the message instead of a popup. it shows in every other client's chat box
Unknown User
Unknown User9mo ago
Message Not Public
Sign In & Join Server To View
zoon_
zoon_OP9mo ago
i se ill check it out people said if u really wanna learn ab sockets and stuff u need to use the real deal not libraries
Jimmacle
Jimmacle9mo ago
signalr is web sockets
zoon_
zoon_OP9mo ago
i see and normal sockets are ?
Jimmacle
Jimmacle9mo ago
they're sockets
zoon_
zoon_OP9mo ago
can i use signalr with winform
Unknown User
Unknown User9mo ago
Message Not Public
Sign In & Join Server To View
Jimmacle
Jimmacle9mo ago
you can, but it's not plain sockets
Unknown User
Unknown User9mo ago
Message Not Public
Sign In & Join Server To View
zoon_
zoon_OP9mo ago
soo as a service ?
Unknown User
Unknown User9mo ago
Message Not Public
Sign In & Join Server To View
zoon_
zoon_OP9mo ago
yes
Unknown User
Unknown User9mo ago
Message Not Public
Sign In & Join Server To View
zoon_
zoon_OP9mo ago
can i still achieve what i want with normal sockets ?
Unknown User
Unknown User9mo ago
Message Not Public
Sign In & Join Server To View
zoon_
zoon_OP9mo ago
yeah 😂
Unknown User
Unknown User9mo ago
Message Not Public
Sign In & Join Server To View
zoon_
zoon_OP9mo ago
will i even benifit anything from doing this
Unknown User
Unknown User9mo ago
Message Not Public
Sign In & Join Server To View
zoon_
zoon_OP9mo ago
.
Unknown User
Unknown User9mo ago
Message Not Public
Sign In & Join Server To View
Jimmacle
Jimmacle9mo ago
i don't understand why you're suggesting websockets
Unknown User
Unknown User9mo ago
Message Not Public
Sign In & Join Server To View
Jimmacle
Jimmacle9mo ago
this looks like a practice application to work with basic networking
Unknown User
Unknown User9mo ago
Message Not Public
Sign In & Join Server To View
Jimmacle
Jimmacle9mo ago
spinning up an entire web application to use websockets instead seems kind of out of scope of the project
Unknown User
Unknown User9mo ago
Message Not Public
Sign In & Join Server To View
zoon_
zoon_OP9mo ago
the goal is both learn and create a chat app
Unknown User
Unknown User9mo ago
Message Not Public
Sign In & Join Server To View
zoon_
zoon_OP9mo ago
but i found 0 good source for learning sockets so i just dived in lmao and i have had so many bugs
Unknown User
Unknown User9mo ago
Message Not Public
Sign In & Join Server To View
zoon_
zoon_OP9mo ago
do you have good sources atleast ? youtube videos ? i'd be thankful
Unknown User
Unknown User9mo ago
Message Not Public
Sign In & Join Server To View
zoon_
zoon_OP9mo ago
ill check that out
Unknown User
Unknown User9mo ago
Message Not Public
Sign In & Join Server To View
zoon_
zoon_OP9mo ago
i am not thats why ive been avoiding it
Unknown User
Unknown User9mo ago
Message Not Public
Sign In & Join Server To View
zoon_
zoon_OP9mo ago
yeah i tried using async it told me no void allowed so i just removed tha async part lmao
Unknown User
Unknown User9mo ago
Message Not Public
Sign In & Join Server To View
zoon_
zoon_OP9mo ago
i actually tried it and it told me i cant use void return type with async
Unknown User
Unknown User9mo ago
Message Not Public
Sign In & Join Server To View
zoon_
zoon_OP9mo ago
wait wdym ?
Unknown User
Unknown User9mo ago
Message Not Public
Sign In & Join Server To View
zoon_
zoon_OP9mo ago
yeah
Unknown User
Unknown User9mo ago
Message Not Public
Sign In & Join Server To View
Unknown User
Unknown User9mo ago
Message Not Public
Sign In & Join Server To View
zoon_
zoon_OP9mo ago
alright ill check it out
Unknown User
Unknown User9mo ago
Message Not Public
Sign In & Join Server To View
zoon_
zoon_OP9mo ago
yeah i take the message as string from the textbox and i send it as bytes
Unknown User
Unknown User9mo ago
Message Not Public
Sign In & Join Server To View
zoon_
zoon_OP9mo ago
so it gets encoded at the other end
Unknown User
Unknown User9mo ago
Message Not Public
Sign In & Join Server To View
zoon_
zoon_OP9mo ago
i actually dont i have it set at 1024 for now
Unknown User
Unknown User9mo ago
Message Not Public
Sign In & Join Server To View
zoon_
zoon_OP9mo ago
the messages will be cut and sent in pieces
Unknown User
Unknown User9mo ago
Message Not Public
Sign In & Join Server To View
zoon_
zoon_OP9mo ago
it already happened
Unknown User
Unknown User9mo ago
Message Not Public
Sign In & Join Server To View
zoon_
zoon_OP9mo ago
ohh okay thats smart
Unknown User
Unknown User9mo ago
Message Not Public
Sign In & Join Server To View
zoon_
zoon_OP9mo ago
i didnt even know that existed alright
Unknown User
Unknown User9mo ago
Message Not Public
Sign In & Join Server To View
zoon_
zoon_OP9mo ago
i see
Unknown User
Unknown User9mo ago
Message Not Public
Sign In & Join Server To View
zoon_
zoon_OP9mo ago
i see i see alrighttt one last question
zoon_
zoon_OP9mo ago
in this server.listen
No description
zoon_
zoon_OP9mo ago
does it make the main thread wait until it receives any connection ?
Unknown User
Unknown User9mo ago
Message Not Public
Sign In & Join Server To View
zoon_
zoon_OP9mo ago
or does it keep on going normally
Unknown User
Unknown User9mo ago
Message Not Public
Sign In & Join Server To View
zoon_
zoon_OP9mo ago
ill just add the message length as a prefix 😂
Unknown User
Unknown User9mo ago
Message Not Public
Sign In & Join Server To View
zoon_
zoon_OP9mo ago
then i can just read the prefix and double it
Unknown User
Unknown User9mo ago
Message Not Public
Sign In & Join Server To View
zoon_
zoon_OP9mo ago
i see
Unknown User
Unknown User9mo ago
Message Not Public
Sign In & Join Server To View
zoon_
zoon_OP9mo ago
that is alot of info that i dont understand yet
Unknown User
Unknown User9mo ago
Message Not Public
Sign In & Join Server To View
zoon_
zoon_OP9mo ago
soo can i just set it as that ?
Unknown User
Unknown User9mo ago
Message Not Public
Sign In & Join Server To View
zoon_
zoon_OP9mo ago
i see i see
Unknown User
Unknown User9mo ago
Message Not Public
Sign In & Join Server To View
Want results from more Discord servers?
Add your server