Click here to Skip to main content
15,849,130 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi
i have developed a windows mobile based client app. i only want to send a simple string to the server running on pc via wifi connection.

i have already written the code and abt 2 months back it was working fine on emulator, but recently when i try to execute the code i didnt work on emulator nor on the device on which i deployed it.

when i press the login button the application does nothing and in the output screen i get
"The thread 0xb69a647e has exited with code 0 (0x0)."

It doesn't send any data to the server. but when i run the exe of the code in debug folder it works fine and sends data to the server

i am so confused that if exe is sending data why isnt the data sent through emulator or mobile device?

I am posting the code below...any help would be much appreciated.

using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
namespace DSDC
{
    enum Command
    {
        Login,      //Log into the server
        Logout,     //Logout of the server
        Message,    //Send a text message to all the chat clients
        List,       //Get a list of users in the chat room from the server
        Null        //No command
    }    
    public partial class Form1 : Form
    {
        public Socket clientSocket;
        public EndPoint epServer;
        public string strName;
        int portnum = 0;
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
        }
        private void LOGIN_Click(object sender, EventArgs e)
        {
            strName = textBox1.Text;
            portnum =Convert.ToInt32(textBox3.Text);
            try
            {
                //Using UDP sockets
                clientSocket = new Socket(AddressFamily.InterNetwork,
                    SocketType.Dgram, ProtocolType.Udp);
                //IP address of the server machine
                IPAddress ipAddress = IPAddress.Parse(textBox2.Text);
                //Server is listening on port 1000
                IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, portnum);
                epServer = (EndPoint)ipEndPoint;
                Data msgToSend = new Data();
                msgToSend.cmdCommand = Command.Login;
                msgToSend.strMessage = null;
                msgToSend.strName = strName;
                byte[] byteData = msgToSend.ToByte();
                //Login to the server
                clientSocket.BeginSendTo(byteData, 0, byteData.Length,
                    SocketFlags.None, epServer, new AsyncCallback(OnSend), null);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "SGSclient");
            } 
        }//end of login button
        private void OnSend(IAsyncResult ar)
        {
            try
            {
                
           
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "SGSclient");
            }
        }//end of on send

    }
    class Data
    {
        //Default constructor
        public Data()
        {
            this.cmdCommand = Command.Null;
            this.strMessage = null;
            this.strName = null;
        }
        //Converts the bytes into an object of type Data
        public Data(byte[] data)
        {
            //The first four bytes are for the Command
            this.cmdCommand = (Command)BitConverter.ToInt32(data, 0);
            //The next four store the length of the name
            int nameLen = BitConverter.ToInt32(data, 4);
            //The next four store the length of the message
            int msgLen = BitConverter.ToInt32(data, 8);
            //This check makes sure that strName has been passed in the array of bytes
            if (nameLen > 0)
                this.strName = Encoding.UTF8.GetString(data, 12, nameLen);
            else
                this.strName = null;
            //This checks for a null message field
            if (msgLen > 0)
                this.strMessage = Encoding.UTF8.GetString(data, 12 + nameLen, msgLen);
            else
                this.strMessage = null;
        }
        //Converts the Data structure into an array of bytes
        public byte[] ToByte()
        {
            List<byte> result = new List<byte>();
            //First four are for the Command
            result.AddRange(BitConverter.GetBytes((int)cmdCommand));
            //Add the length of the name
            if (strName != null)
                result.AddRange(BitConverter.GetBytes(strName.Length));
            else
                result.AddRange(BitConverter.GetBytes(0));
            //Length of the message
            if (strMessage != null)
                result.AddRange(BitConverter.GetBytes(strMessage.Length));
            else
                result.AddRange(BitConverter.GetBytes(0));
            //Add the name
            if (strName != null)
                result.AddRange(Encoding.UTF8.GetBytes(strName));
            //And, lastly we add the message text to our array of bytes
            if (strMessage != null)
                result.AddRange(Encoding.UTF8.GetBytes(strMessage));
            return result.ToArray();
        }
        public string strName;      //Name by which the client logs into the room
        public string strMessage;   //Message text
        public Command cmdCommand;  //Command type (login, logout, send message, etcetera)
    }
}
Posted
Updated 20-Sep-10 7:51am
v2
Comments
TheyCallMeMrJames 20-Sep-10 12:51pm    
whitespace and code formatting are your friends ;o)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900