请教下,这个该如何写?

睡神在睡觉 2010-05-16 06:30:03
在socket通信中连接建立好,怎么才能实现让客户端和服务器端一直监听端口?除了用timer,我不知道一般的软件,譬如qq之类的即时通信软件用的是不是类似TImer的东西刷的,我感觉应该不会,太弱智了点吧。。。。请教下,如何实现的?(我也不知道我说的清楚不清楚,就是类似于串口通信中的DataReceived方法似的,打开串口,这个监听一直存在,只要串口有数据上来就会收到,像这样的怎么能实现?)
...全文
89 点赞 收藏 11
写回复
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
这个刷似乎……
打开一个监听一个
回复
发动态
发帖子
C#
创建于2007-09-28

10.4w+

社区成员

.NET技术 C#
申请成为版主
社区公告

全世界最好的语言,没有之一.