TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Nesan Xean
NA
3
2.8k
how to receive data in tcpServer\listener to textbox?
May 20 2013 10:02 AM
Hi everyone:
I have a tcpClient and tcpListener and i want to know how can i recive data from the client to the server and display it in a textbox in the server windows form.
Code:
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;
using System.Net.Sockets;
using System.Threading;
namespace tcpServer
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public TcpListener m_Socket;
//private TcpClient[] m_Client;
private TcpClient m_Client;
//int count = 0;
private void btnConnect_Click(object sender, EventArgs e)
{
Thread acceptClients = new Thread(new ThreadStart(acceptConnections));
acceptClients.IsBackground = true;
acceptClients.Start();
}
//////////////////////
//accept connection
////////////////
private void acceptConnections()
{
try
{
IPAddress ipConfig = IPAddress.Parse("127.0.0.1");
m_Socket = new TcpListener(ipConfig, 1120);
m_Socket.Start();
updInfo("listening");
//while (true)
//{
// m_Client[count] = m_Socket.AcceptTcpClient();
// sendData(m_Client[count], "connected");
// updInfo("new client");
// count++;
//}
while (true)
{
m_Client = m_Socket.AcceptTcpClient();
sendData(m_Client, "connected");
updInfo("new client");
}
}
catch (Exception error)
{
updInfo(error.Message);
}
}
//////////////////////////
//Update Info
/////////////////////////
public void updInfo(String textLog)
{
if (this.txtRecv.InvokeRequired)
{
txtRecv.Invoke(new MethodInvoker(delegate { txtRecv.Text += textLog + "\n"; }));
}
else
{
this.txtRecv.Text = textLog;
}
}
/////////////////////////
//Send Data
////////////////////////
private void sendData(TcpClient soc, String strData)
{
try
{
if (soc.Connected && soc.Client.Poll(3000, SelectMode.SelectWrite))
{
{
char c = (char)0;
byte[] msg = System.Text.Encoding.ASCII.GetBytes(strData + c);
int i = soc.Client.Send(msg, 0, msg.Length, SocketFlags.None);
}
}
else
{
}
}
catch
{
}
}
//////////////////////////
///////Receive Data//////
////////////////////////
private void btnRecv_Click(object sender, EventArgs e)
{
// You could also user server.AcceptSocket() here.
TcpClient client = m_Socket.AcceptTcpClient();
// Get a stream object for reading and writing
NetworkStream stream = client.GetStream();
int i;
// Buffer for reading data
Byte[] bytes = new Byte[256];
String data = null;
// Loop to receive all the data sent by the client.
while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
{
// Translate data bytes to a ASCII string.
data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
Console.WriteLine("Received: {0}", data);
// Process the data sent by the client.
data = data.ToUpper();
byte[] msg = System.Text.Encoding.ASCII.GetBytes(data);
// Send back a response.
stream.Write(msg, 0, msg.Length);
textBox1.Text = data;
//Console.WriteLine("Sent: {0}", data);
}
}
}
}
Reply
Answers (
1
)
what is c#
How can I draw image in panel when I click mouse up ?