开发者

manually input in textbox and connected to the server

Right now, when i press the connect button i will be connected to the server by the default ip address and port number. clientSocket.Connect("127.0.0.1", 8888);

I would like to create 2 textbox in the GUI , 1 for the IP address and 1 for port. So that user can manually key in the IP add and port. May i know how to do this. thanks.

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Net.Sockets; using System.Threading;

namespace SocketClient {

public partial class SocketClient : Form
{
    System.Net.Sockets.TcpClient clientSocket = new System.Net.Sockets.TcpClient();
    NetworkStream serverStream = default(NetworkStream);
    string readData = null;


    public SocketClient()
    {
        InitializeComponent();
    }


    private void getMessage()
    {
        while (true)
        {
            serverStream = clientSocket.GetStream();
            int buffSize = 0;
            byte[] inStream = new byte[10025];
            buffSize = clientSocket.ReceiveBufferSize;
            serverStream.R开发者_C百科ead(inStream, 0, buffSize);
            string returndata = System.Text.Encoding.ASCII.GetString(inStream);
            readData = "" + returndata;
            msg();
        }
    }


    private void msg()
    {
        if (this.InvokeRequired)
            this.Invoke(new MethodInvoker(msg));
        else
            textDisplay.Text = textDisplay.Text + Environment.NewLine + " >> " + readData;
    }


    private void buttonConnect_Click(object sender, EventArgs e)
    {
        readData = "Conected to NYP Chat Server ...";
        msg();
        //
        clientSocket.Connect("127.0.0.1", 8888);
        serverStream = clientSocket.GetStream();

        byte[] outStream = System.Text.Encoding.ASCII.GetBytes(textName.Text + "$");
        serverStream.Write(outStream, 0, outStream.Length);
        serverStream.Flush();

        Thread ctThread = new Thread(getMessage);
        ctThread.Start();
    }


    private void buttonSend_Click(object sender, EventArgs e)
    {

    }

    private void textDisplay_TextChanged(object sender, EventArgs e)
    {

    }
}

}


In the designer view, add two TextBoxes in the desired positions and name the texbboxes as tbIp and tbPort.

update the line following line clientSocket.Connect("127.0.0.1", 8888); to clientSocket.Connect(tbIp.Text, Convert.Int32(tbPort.Text));

Regards ArunDhaJ

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜