新手求教:关于C# socket
各位大哥,小弟刚接触C#,现有几个比较弱的socket小问题急于请教,客户端代码如下:namespace xxxx
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Socket sk = null; // 这里如何设置?
public void btn1_Click(object sender, EventArgs e)
{
try
{
int port = xxxx;
string host = "xxx.xxx.x.xx";
IPAddress ip = IPAddress.Parse(host);
IPEndPoint endpt = new IPEndPoint(ip, port)
Socket sk = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
sk.Connect(endpt);
}
catch (SocketException se)
{
MessageBox.Show(se.Message);
}
}
//以下事件是接受从服务器端返回的信息。如果服务端有一数据源,不停地发送数据,如何将这些数据显示在这个客户端的文本框内?
public void richTextBox1_TextChanged(object sender, EventArgs e)
{
string recvStr = "";
byte[] recvBytes = new byte[1024];
int bytes;
bytes = sk.Receive(recvBytes, recvBytes.Length, 0);//
recvStr += Encoding.ASCII.GetString(recvBytes, 0, bytes);
richTextBox1.Text = recvStr;
}
private void btn3_Click(object sender, EventArgs e)
{
sk.Close(); //如何设置这个按钮?C#有全局变量吗?
}
}
}
-------------
问题写在注释里面了。 希望得到大家的帮助,非常感谢!!
--------------------编程问答-------------------- 如果能有代码帮助就更好了。或者是服务端代码。 --------------------编程问答-------------------- 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();
}
}
--------------------编程问答-------------------- --------------------编程问答-------------------- --------------------编程问答-------------------- 晕,看不懂楼主写的啥。
在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.
--------------------编程问答--------------------
谢谢你的代码。
请问客户端如何更新接收到的文本区的内容? --------------------编程问答--------------------
好像没有接收信息...? --------------------编程问答-------------------- 在UI中操作socket,应使用异步操作;对于用socket接收与发送数据,可以参考SDK中的帮助信息,流程如下:
(针对于TCP)
1、服务器端:
创建SOCKET对象,绑定端口,开始监听客户端连接请求,当客户端有连接请求后开始接收数据
2、客户端:
创建SOCKET对象,请求连接服务器,连接后就可以发送数据了 --------------------编程问答-------------------- 窈窕淑女,君子好逑 --------------------编程问答--------------------
补充:.NET技术 , C#