using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;class MyTcpListener
{
public static void Main()
{
TcpListener server=null;
try
{
// Set the TcpListener on port 13000.
Int32 port = 13000;
IPAddress localAddr = IPAddress.Parse("127.0.0.1"); // TcpListener server = new TcpListener(port);
server = new TcpListener(localAddr, port); // Start listening for client requests.
server.Start(); // Buffer for reading data
Byte[] bytes = new Byte[256];
String data = null; // Enter the listening loop.
while(true)
{
Console.Write("Waiting for a connection... "); // Perform a blocking call to accept requests.
// You could also user server.AcceptSocket() here.
TcpClient client = server.AcceptTcpClient();
Console.WriteLine("Connected!"); data = null; // Get a stream object for reading and writing
NetworkStream stream = client.GetStream(); int i; // 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);
Console.WriteLine("Sent: {0}", data);
} // Shutdown and end connection
client.Close();
}
}
catch(SocketException e)
{
Console.WriteLine("SocketException: {0}", e);
}
finally
{
// Stop listening for new clients.
server.Stop();
}
Console.WriteLine("\nHit enter to continue...");
Console.Read();
}
}
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;class MyTcpListener
{
public static void Main()
{
TcpListener server=null;
try
{
// Set the TcpListener on port 13000.
Int32 port = 13000;
IPAddress localAddr = IPAddress.Parse("127.0.0.1"); // TcpListener server = new TcpListener(port);
server = new TcpListener(localAddr, port); // Start listening for client requests.
server.Start(); // Buffer for reading data
Byte[] bytes = new Byte[256];
String data = null; // Enter the listening loop.
while(true)
{
Console.Write("Waiting for a connection... "); // Perform a blocking call to accept requests.
// You could also user server.AcceptSocket() here.
TcpClient client = server.AcceptTcpClient();
Console.WriteLine("Connected!"); data = null; // Get a stream object for reading and writing
NetworkStream stream = client.GetStream(); int i; // 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);
Console.WriteLine("Sent: {0}", data);
} // Shutdown and end connection
client.Close();
}
}
catch(SocketException e)
{
Console.WriteLine("SocketException: {0}", e);
}
finally
{
// Stop listening for new clients.
server.Stop();
}
Console.WriteLine("\nHit enter to continue...");
Console.Read();
}
}
解决方案 »
- VB,VC++,C#编译执行的区别?
- C# 调用API创建窗口
- 用VS2005做串口程序时,ComBox内内容改不掉是怎么回事?
- 如何确定一个字符串的长度是多少像素
- c# web编程求助
- 一个关于session丢失的怪问题
- Socket编程的一个问题,请教下大家
- 请问:用C#编写的程序能在98下运行吗??
- Repeter连接到数据源的问题.............................................多多指教(给分)
- 结构体的初值是什么,如何知道DateTime未被赋值
- 如何给自定义控件加入MouseDown、MouseMove、及MouseUp事件实现拖拽效果
- 数据库脚本导入错误
在richTextBox1_TextChanged改变richTextBox1的Text值,
你应该在Socket的ReceviceData时异步改变richTextBox1的Text值,Socket实列定义在函数外,不就是类中的全局变量吗
using System.Net;using System.Net.Sockets;using System.Text; private void button1_Click(object sender, System.EventArgs e) { BeginSend(); } private void BeginSend() { string ip=this.txtip.Text; string port=this.txtport.Text; IPAddress serverIp=IPAddress.Parse(ip); int serverPort=Convert.ToInt32(port); IPEndPoint iep=new IPEndPoint(serverIp,serverPort); byte[] byteMessage; // do// { Socket socket=new Socket(AddressFamily.Inte.Network,SocketType.Stream,ProtocolType.Tcp); socket.Connect(iep); byteMessage=Encoding.ASCII.GetBytes(textBox1.Text); socket.Send(byteMessage); socket.Shutdown(SocketShutdown.Both); socket.Close();// }// while(byteMessage!=null); }
请问应该如何设置异步改变richTextBox1_TextChanged, 来update收来的不停的数据?
但我按断开连接的那个按钮btn3会出现 Object reference not set to an instance of an object.
(针对于TCP)
1、服务器端:
创建SOCKET对象,绑定端口,开始监听客户端连接请求,当客户端有连接请求后开始接收数据
2、客户端:
创建SOCKET对象,请求连接服务器,连接后就可以发送数据了