I have written a client and server application that I need to use to connect a checkers game I made in C#. I have got the client and server to connect and the server can repeatedly send the client messages to update a label but when the client tries to send a message it throws the error
"A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using a sendto call) no address was supplied."
Here is my client and server so far.
CLIENT -
public partial class Form1 : Form //main form that establishes connection
{
Form2 form2 = new Form2();
Socket sck = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
Socket acc = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint endPoint;
static byte[] Buffer { get; set; }
string text;
public Form1()
{
InitializeComponent();
}
private void button1_Click_1(object sender, EventArgs e)
{
Thread rec = new Thread(recMsg);
Thread t = new Thread(ThreadProc);
t.Start(); //starts a form that will call the sendMsg on a button click
endPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8887);
try
{
sck.Connect(endPoint);
}
catch
{
Console.WriteLine("unable to connect");
}
rec.Start();
text = textBox1.Text;
sendMsg(text);
}
public void sendMsg(string s)
{
//bool x = true;
//while (true)
//{
//Thread.Sleep(500);
//if (x == true)
//{
byte[] msgBuffer = Encoding.ASCII.GetBytes(s);
sck.Send(msgBuffer); //error comes here when i try to send a message from form2 says it isn't connected and has no address
// x = false;
//}
//} the commented out part doesn't effect how the send works it sends once and can't again, I think the problem is that the thread which establishes the connection dies but don't know how to solve.
}
public void recMsg()
{
while (true)
{
Thread.Sleep(500);
byte[] Buffer = new byte[255];
int rec = sck.Receive(Buffer, 0, Buffer.Length, 0);
Array.Resize(ref Buffer, rec);
form2.SetText(Encoding.Default.GetString(Buffer));
}
}
private void button2_Click(object sender, EventArgs e)
{
sck.Close();
}
public void ThreadProc()
{
form2.ShowDialog();
}
}
Form 2 that has a label1, textbox1 and button1, main operating form that will take input and call the Form1 sendMsg()
public partial class Form2 : Form
{
delegate void SetTextCallback(string text);
Form1 form1;
public Form2()
{
InitializeComponent();
}
public void SetText(string text)
{
if (InvokeRequired)
{
SetTextCallback d = new SetTextCallback(SetText);
Invoke(d, new object[] { text });
}
else
{
label1.Text = text;
}
}
private void button1_Click(object sender, EventArgs e)
{
form1 = new Form1();
form1.sendMsg(textBox1.Text);
}
}
SERVER-
class Program
{
static Form1 form1 = new Form1();
static Form2 form2 = new Form2();
static byte[] buffer { get; set; }
static Socket sck, acc;
static string name;
static void Main(string[] args)
{
if (name == null)
{
Thread t = new Thread(ThreadProc);
t.Start();
}
Thread rec = new Thread(recMsg);
sck = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
sck.Bind(new IPEndPoint(0, 8887));
Console.WriteLine("Your local IP address is: " + getIP());
Console.WriteLine("Awaiting Connection");
sck.Listen(0);
acc = sck.Accept();
Console.WriteLine(" >> Accept connection from client");
rec.Start();
sendMsg();
}
static string getIP()
{
string hostName = System.Net.Dns.GetHostName();
IPHostEntry ipEntry = System.Net.Dns.GetHostEntry(hostName);
IPAddress[] addr = ipEntry.AddressList;
return addr[addr.Length - 1].ToString();
}
static void recMsg()
{
while (acc.Connected)
{
Thread.Sleep(500);
byte[] Buffer = new byte[255];
int rec = acc.Receive(Buffer, 0, Buffer.Length, 0);
Array.Resize(ref Buffer, rec);
form2.SetText(Encoding.Default.GetString(Buffer));
}
}
public void btnClick(string s)
{
name = s;
Console.WriteLine("Name: " + name);
System.Threading.Thread r = new System.Threading.Thread(new System.Threading.ThreadStart(ThreadProc2));
r.Start();
}
public void sendMSG(string s)
{
try
{
buffer = Encoding.Default.GetBytes(s);
acc.Send(buffer);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
static void sendMsg()
{
try
{
buffer = Encoding.Default.GetBytes(name);
acc.Send(buffer);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
public static void ThreadProc()
{
form1.ShowDialog();
}
public static void ThreadProc2()
{
form2.ShowDialog();
}
}
Form1 - starter form that asks for a name, this form could be scrathed just used to build upon my basic server which will eventually be needed for the checkers game.
public partial class Form1 : Form
{
Program program;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
this.Hide();
program = new Program();
program.btnClick(textBox1.Text);
}
}
Form2 used as an interface like Form2 of my client that basically has a label, textbox and button that calls the sendMsg()
public partial class Form2 : Form
{
delegate void SetTextCallback(string text);
Program program = new Program();
public Form2()
{
InitializeComponent();
}
public void SetText(string text)
{
if (InvokeRequired)
{
SetTextCallback d = new SetTextCallback(SetText);
this.Invoke(d, new object[] { text });
}
else
{
this.label2.Text = text;
}
}
private void button1_Click(object sender, EventArgs e)
{
program.sendMSG(textBox1.Text);
}
}
To recap, this program will connect and the server can send data multiple times to the client and update a label. The client will send data to the server the first time and then it throws the error.