请教下,这个该如何写?

睡神在睡觉 2010-05-16 06:30:03
在socket通信中连接建立好,怎么才能实现让客户端和服务器端一直监听端口?除了用timer,我不知道一般的软件,譬如qq之类的即时通信软件用的是不是类似TImer的东西刷的,我感觉应该不会,太弱智了点吧。。。。请教下,如何实现的?(我也不知道我说的清楚不清楚,就是类似于串口通信中的DataReceived方法似的,打开串口,这个监听一直存在,只要串口有数据上来就会收到,像这样的怎么能实现?)
...全文
151 11 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
睡神在睡觉 2010-05-17
  • 打赏
  • 举报
回复
呵呵,谢谢楼上两位,昨天我自己琢磨着用多线程做,但是还是去试,晚上回家试试。
xk1126 2010-05-16
  • 打赏
  • 举报
回复
const int portNumber = 13;
TcpListener tcpListener = new TcpListener(portNumber);

tcpListener.Start();

Console.WriteLine("Waiting for a connection....");

try{

//Accept the pending client connection and return a TcpClient initialized for communication.
TcpClient tcpClient = tcpListener.AcceptTcpClient();
Console.WriteLine("Connection accepted.");

NetworkStream networkStream = tcpClient.GetStream();

string responseString = "You have successfully connected to me";

Byte[] sendBytes = Encoding.ASCII.GetBytes(responseString);
networkStream.Write(sendBytes, 0, sendBytes.Length);

Console.WriteLine("Message Sent /> : " + responseString);

//Any communication with the remote client using the TcpClient can go here.
//
////////

//Close TcpListener and TcpClient.
tcpClient.Close();
tcpListener.Stop();

}
catch (Exception e) {
Console.WriteLine(e.ToString());
}
yuquan1987 2010-05-16
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
CheckForIllegalCrossThreadCalls = false;
}

Socket ClientServer;//定义全局变量
/// <summary>
/// 连接
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
Thread th = new Thread(new ThreadStart(start));
th.IsBackground = true;
th.Start();
}

private void button2_Click(object sender, EventArgs e)
{
byte[] Data = UnicodeEncoding.UTF8.GetBytes(this.textBox4.Text+"说:"+this.textBox3.Text+"\n");
ClientServer.Send(Data);
this.richTextBox1.AppendText(this.textBox3.Text+"\n");
}
void start()
{
IPAddress ip=IPAddress.Parse(this.textBox1.Text);
int port=int.Parse(this.textBox2.Text);
IPEndPoint MyIPEnd = new IPEndPoint(ip, port);
ClientServer = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
try
{
ClientServer.Connect(MyIPEnd);
MessageBox.Show("连接成功!");
this.button2.Enabled = true;
this.toolStripStatusLabel1.Text = "与服务器建立了连接!";
while (true)
{
byte[] msg = new byte[1024];
int i = ClientServer.Receive(msg);
string str = UnicodeEncoding.UTF8.GetString(msg, 0, i);
this.richTextBox1.AppendText(str);
}

}
catch
{
MessageBox.Show("无法建立连接!");
}

}

private void Form1_Load(object sender, EventArgs e)
{
this.button2.Enabled = false;
}
}
}










yuquan1987 2010-05-16
  • 打赏
  • 举报
回复
我找找
yuquan1987 2010-05-16
  • 打赏
  • 举报
回复
我有点忘记了。记得当初我们做了一个局域网聊天的程序。
wuyq11 2010-05-16
  • 打赏
  • 举报
回复
noway8881 2010-05-16
  • 打赏
  • 举报
回复
codeproject上有很多
自己搜索下

An Asynchronous Socket Server and Client
By Andre Azevedo
An asynchronous socket server and client with encryption and compression.
http://www.codeproject.com/KB/IP/AsyncSocketServerandClien.aspx

A Chat Application Using Asynchronous UDP sockets
By Hitesh Sharma
This article shows how to develop a chat application using UDP sockets.
http://www.codeproject.com/KB/IP/ChatAppAsynchUDPSocks.aspx
睡神在睡觉 2010-05-16
  • 打赏
  • 举报
回复
呵呵,我只是自己做着玩的,类似于qq这种网络通信的小东西,感觉还到不ly302说的这个地步吧,纯属个人感觉,
皇城龙三 2010-05-16
  • 打赏
  • 举报
回复
我觉得从软件效率的角度考虑

还是用VC加汇编作这个比较好

c#对这种截获数据包的操作

性能相对还是低一些

如果非要用c#作,还是做个服务吧,当个守护进程放在客户端
睡神在睡觉 2010-05-16
  • 打赏
  • 举报
回复
我的意思是,如何能让接收数据和操作数据的步骤不停循环运行,前提是有新数据上来的时候
空心兜兜 2010-05-16
  • 打赏
  • 举报
回复
这个刷似乎……
打开一个监听一个

111,093

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧