I'm struggling to get this to work.
I have 2 errors coming up,
Exception thrown: 'System.Net.Sockets.SocketException' in System.dll
Exception thrown: 'System.FormatException' in mscorlib.dll
Any help will be greatly appreciated! (Iv'e read other posts and they don't seem to help)
This is the error message:
public Socket_Chat()
{
InitializeComponent();
// Initialize Component Methods using the variable sck.
// From the socket fucntion it takes 3 arguments and gives them a type.
sck = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
sck.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
// Setting the client variables for the IP Addresses
textLocalIP.Text = GetLocalIP();
textFriendIP.Text = GetLocalIP();
}
// Creation of a new method to get IP off host
// This will automatically get the Local IP for the IP text Boxes so the user won't need to manually enter the IP.
private string GetLocalIP()
{
IPHostEntry host;
host = Dns.GetHostEntry(Dns.GetHostName());
foreach (IPAddress ip in host.AddressList)
{
if (ip.AddressFamily == AddressFamily.InterNetwork)
{
return ip.ToString();
}
}
// Else returns my local host IP address
return "127.0.0.1";
}
private void MessageCallBack(IAsyncResult aResult)
{
try
{
int size = sck.EndReceiveFrom(aResult, ref epRemote);
if (size > 0)
{
byte[] recieveData = new byte[1464];
receivedData = (byte[])aResult.AsyncState;
ASCIIEncoding eEncoding = new ASCIIEncoding();
string receivedMessage = eEncoding.GetString(receivedData);
listMessage.Items.Add("Friend: "+receivedMessage);
}
byte[] buffer = new byte[1500];
sck.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref epRemote, new AsyncCallback(MessageCallBack), buffer);
}
catch (Exception exp)
{
MessageBox.Show(exp.ToString());
}
}
private void button1_Click(object sender, EventArgs e)
{
Programz test = new Programz();
test.Show();
Visible = false;
}
private void groupBox2_Enter(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
try
{
epLocal = new IPEndPoint(IPAddress.Parse(textLocalIP.Text), Convert.ToInt32(textLocalPort.Text));
sck.Bind(epLocal);
epRemote = new IPEndPoint(IPAddress.Parse(textFriendIP.Text), Convert.ToInt32(textFriendPort.Text));
sck.Bind(epRemote);
byte[] buffer = new byte[1500];
sck.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref epRemote, new AsyncCallback(MessageCallBack), buffer);
button2.Text = "Connected";
button2.Enabled = false;
button3.Enabled = true;
// Sets the focus on the message box
textMessage.Focus();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
private void button3_Click(object sender, EventArgs e)
{
try
{
System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
byte[] msg = new byte[1500];
msg = enc.GetBytes(textMessage.Text);
sck.Send(msg);
// Shows the message that I am sending
listMessage.Items.Add("You"+ textMessage.Text);
// Clears the text message
textMessage.Clear();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
private void Socket_Chat_Load(object sender, EventArgs e)
{
}
}
}
Edit: And now this error comes up:
This is what comes up when I debug:
'WindowsFormsApp2.exe' (CLR v4.0.30319: WindowsFormsApp2.exe): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Management\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Management.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
The thread 0x3b38 has exited with code 0 (0x0). 'WindowsFormsApp2.exe' (CLR v4.0.30319: WindowsFormsApp2.exe): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Numerics\v4.0_4.0.0.0__b77a5c561934e089\System.Numerics.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled. Exception thrown: 'System.ArgumentNullException' in System.dll The program '[14928] WindowsFormsApp2.exe' has exited with code -1 (0xffffffff).
User contributions licensed under CC BY-SA 3.0