100分求网络聊天源码用socket的 简单的实现就行,主要想了解服务器和客户端如何通过一个连接,发送和接收信息(异步\C#)我的E_mail:[email protected]谢谢各位大侠了.. 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 这个通讯聊天的客户端!using System;using System.Net.Sockets;using System.Text;public class TcpTimeClient{ private const int portNum = 1555; private const string hostName = "localhost"; public static int Main(String[] args) { while (true) { try { Console.Write("Try to connect to " + hostName + ":" + portNum.ToString() + "\r\n"); TcpClient client = new TcpClient(hostName, portNum); while (true) { NetworkStream ns = client.GetStream(); byte[] bytes = new byte[1024]; int bytesRead = ns.Read(bytes, 0, bytes.Length); Console.WriteLine(Encoding.ASCII.GetString(bytes, 0, bytesRead)); } string message=Console.ReadLine(); if (message == "stop") { client.Close(); } } catch (Exception e) { Console.WriteLine(e.ToString()); Console.ReadLine(); } return 0; } } 这是通讯源代码的服务端!using System; using System.Net.Sockets; using System.Text; namespace TimeServer { class TimeServer { private const int portNum = 1555; private const string hostName = "localhost"; [STAThread] static void Main(string[] args) { bool done = false; TcpListener listener = new TcpListener(portNum); listener.Start(); Console.Write("Waiting for connection..."); TcpClient client = listener.AcceptTcpClient(); while (!done) { Console.WriteLine("Connection accepted."); // Socket soo = listener.AcceptSocket(); // if( soo.Connected) //{ // string ServerEndPoint = "服务端地址" + soo.LocalEndPoint.ToString() + "\r\n"; //string mpoint = "请求的客户端地址用端口"+soo.RemoteEndPoint.ToString(); //Console.WriteLine(ServerEndPoint); //Console.WriteLine (mpoint); // Console.ReadLine(); //} NetworkStream ns = client.GetStream(); Console.WriteLine("请输入你要发送的信息"); string s = Console.ReadLine(); if (s =="pouse") { ns.Close(); client.Close(); } try { byte[] byteTime = Encoding.ASCII.GetBytes(s.ToString()); ns.Write(byteTime, 0, byteTime.Length); } catch (Exception e) { Console.WriteLine(e.ToString()); } } } } } 这是用c#控制台应用程序写的,代码结构清晰,如有什么不懂的可以发e-mail与我联系,[email protected] 简单的实现就行,主要想了解服务器和客户端如何通过一个连接,发送和接收信息(异步\C#) (使用socket)客户端收到信息就显示到textbox中,发送信息和接收信息都使用同一连接. 我有一个TCP 一个UTP的 不知道你要哪个 SQL语句的传值问题 安装Sql Server 2008专业版后碰到的问题 判断是否联网 C#调用C++动态链接库函数,有指针 怎么调用? 需要很多次str1+=str2操作,有没有这比+=这方式节省资源的方法呢? ListView如何分栏显示 妈的,老子忍不住想骂,比尔.盖茨,什么吊C#新语言,语言清析透明. VS2008在与SQL插入时出现Connectionstring未初始化问题 outlook自动填写收件人功能如何实现? 关于c#的一个简单问题 关于DataGridView的数据绑定问题 txtBOX文本框绑定后,为何不随DataGrid,comboBox连动?谢谢指导
using System;
using System.Net.Sockets;
using System.Text;
public class TcpTimeClient
{ private const int portNum = 1555;
private const string hostName = "localhost";
public static int Main(String[] args)
{
while (true)
{
try
{
Console.Write("Try to connect to " + hostName + ":" + portNum.ToString() + "\r\n"); TcpClient client = new TcpClient(hostName, portNum);
while (true)
{
NetworkStream ns = client.GetStream(); byte[] bytes = new byte[1024];
int bytesRead = ns.Read(bytes, 0, bytes.Length);
Console.WriteLine(Encoding.ASCII.GetString(bytes, 0, bytesRead));
}
string message=Console.ReadLine();
if (message == "stop")
{
client.Close();
} }
catch (Exception e)
{
Console.WriteLine(e.ToString());
Console.ReadLine();
}
return 0;
} }
using System;
using System.Net.Sockets;
using System.Text;
namespace TimeServer
{
class TimeServer
{
private const int portNum = 1555;
private const string hostName = "localhost";
[STAThread]
static void Main(string[] args)
{
bool done = false;
TcpListener listener = new TcpListener(portNum);
listener.Start();
Console.Write("Waiting for connection...");
TcpClient client = listener.AcceptTcpClient();
while (!done)
{
Console.WriteLine("Connection accepted."); // Socket soo = listener.AcceptSocket();
// if( soo.Connected)
//{
// string ServerEndPoint = "服务端地址" + soo.LocalEndPoint.ToString() + "\r\n";
//string mpoint = "请求的客户端地址用端口"+soo.RemoteEndPoint.ToString();
//Console.WriteLine(ServerEndPoint);
//Console.WriteLine (mpoint);
// Console.ReadLine();
//}
NetworkStream ns = client.GetStream();
Console.WriteLine("请输入你要发送的信息");
string s = Console.ReadLine();
if (s =="pouse")
{
ns.Close();
client.Close();
}
try
{
byte[] byteTime = Encoding.ASCII.GetBytes(s.ToString());
ns.Write(byteTime, 0, byteTime.Length);
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
}
}
}