- Home /
Problems with UDP data sending
Whenever I run my GUI script, I keep getting the error "SocketException: The requested address is not valid in its context." on line 35 and when I press the "Sign in" button, I get the error "SocketException: An invalid argument was supplied." on line 61. I've been working on this for at least three hours strait to no avail. Hopefully someone else can figure it out!
here's the code:
using UnityEngine;
using System.Collections;
using System.Net;
using System.Net.Sockets;
using System;
using System.Linq;
using System.Text;
public class LoginGUI : MonoBehaviour {
public static int myRecPort;// = 1234;
public static int mySendPort = 1235;
public static string ServerIPAddress = "255.255.255.255";
private Rect loginWindowRect = new Rect (20, 20, 300, 250);
private string UsernameField = "Username";
private string PasswordField = "Password";
private string reply;
//Communication setup
System.Net.Sockets.UdpClient sock = new System.Net.Sockets.UdpClient();
IPEndPoint iep = new IPEndPoint(IPAddress.Parse("255.255.255.255"), 1235);
byte[] SendData = new byte[9999];
//recieve
int recv;
byte[] RecData = new byte[9999];
IPEndPoint endpoint = new IPEndPoint(IPAddress.Parse("255.255.255.255"), 1234);
Socket newSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
IPEndPoint sender = new IPEndPoint(IPAddress.Parse("255.255.255.255"), 1234);
EndPoint tmpRemote;
void Start () {
tmpRemote = (EndPoint)sender;
newSocket.Bind(endpoint);
Debug.Log("connected");
}
void OnGUI () {
loginWindowRect = GUI.Window (0, loginWindowRect, LoginWindow, "Login");
}
void LoginWindow (int windowID) {
GUI.Label(new Rect (25, 25, 100, 30), "Username: ");
GUI.Label(new Rect (25, 60, 100, 30), "Password: ");
UsernameField = GUI.TextField (new Rect (125, 25, 150, 20), UsernameField);
PasswordField = GUI.PasswordField (new Rect (125, 60, 150, 20), PasswordField, "*"[0]);
if (GUI.Button (new Rect(25, 100, 250, 30), "Sign in")) {
reply = SendReceive("Login(" + UsernameField + ")[" + PasswordField + "]");
Debug.Log("sent login");
}
GUI.DragWindow(new Rect(0, 0, 10000, 10000));
}
string SendReceive(string message)
{
SendData = Encoding.ASCII.GetBytes(message);
sock.Send(SendData, SendData.Length, iep);
recv = newSocket.ReceiveFrom(RecData, ref tmpRemote);
return Encoding.ASCII.GetString(RecData);
}
}
Answer by spike35031 · Jun 11, 2012 at 05:03 AM
Somehow got it to work by copy-pasting it into a new class file. Not sure why it worked though...
Your answer
Follow this Question
Related Questions
UDP Sockets for networking, getting overloaded?? 0 Answers
Connect to non-unity server with LLAPI 0 Answers
Networking source for new networking example 0 Answers
How do I identify if the packet arrived by UDP or TCP protocol 1 Answer
Which ways are available for a Unity program to communicate with other programs? 2 Answers