-
March 24th, 2011, 12:50 AM
#1
tcp socket server application
Guys,
I am trying to create a GUI socket server.
The goal is simple.
1.Open a port
2.keep sending data to the port
3.the data is given from a textbox in the gui and must be sent after the button is pressed.
4.Do NOT CLOSE THE CONNECTION to the port until a button is pressed in the GUI.
I have done some part of it but the problem i m facing with my code is i am not able to split the accept client connection part from the send data to client part.
Hence each time i click my button to send the data i have to close the connection and open a new connection again. Please help me with this. The code below executes on button press. As you can see each time i have to close the socket and redo everything on each button press. I am not able to split it into parts. Please help me.
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint ip = new IPEndPoint(IPAddress.Any, 9999);
socket.Bind(ip);
socket.Listen(10);
Console.WriteLine("Waiting for a client...");
Socket client = socket.Accept();
IPEndPoint clientep = (IPEndPoint)client.RemoteEndPoint;
cliStatusLbl.Text = "Connected with Client";
string sndCmd = "|" + this.keyTxt + "|" + this.valTxt + "\n";
byte[] data = new byte[1024];
data = Encoding.ASCII.GetBytes(sndCmd);
client.Send(data, data.Length, SocketFlags.None);
Thread.Sleep(2000);
client.Close();
socket.Close();
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|