x


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);
}
}
more ▼

asked Jun 11 '12 at 12:09 AM

spike35031 gravatar image

spike35031
0 1 1 2

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

Somehow got it to work by copy-pasting it into a new class file. Not sure why it worked though...

more ▼

answered Jun 11 '12 at 05:03 AM

spike35031 gravatar image

spike35031
0 1 1 2

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x707
x700
x53
x30
x15

asked: Jun 11 '12 at 12:09 AM

Seen: 727 times

Last Updated: Jun 11 '12 at 05:03 AM