通讯聊天
来源:互联网 发布:知乎励志的话 编辑:程序博客网 时间:2024/04/28 23:07
客户端:
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() + " ");
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.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() + " ");
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() + " ";
//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());
}
}
}
}
}
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() + " ";
//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());
}
}
}
}
}
- 通讯聊天
- QQ聊天通讯原理
- Socket TCP聊天通讯
- Android 通讯聊天软件介绍
- Android蓝牙聊天,蓝牙通讯
- ios 视频聊天 视频通讯
- 集趣(及时通讯聊天)
- 多线程通讯-TCP聊天程序-VC++
- 点对面的通讯(Dos版聊天)
- 点对面的通讯(Dos版聊天) .
- Linux下socket异步通讯聊天程序
- android 人机交互聊天,数据封装,异步通讯
- 第三方视频聊天解决----云通讯
- 聊天类型的通讯界面listView总结
- Linux下socket异步通讯聊天程序
- WCF双工通讯实现聊天传文件
- Android Java_WebSocket实现与后台聊天通讯
- Linux网络编程一步一步学-异步通讯聊天程序select
- 一段显示下载进度条的下载文件代码
- Telnet服務的配置
- Spring 2.0集成Tapestry 5.0
- 基于SSL的Webmin的安裝配置
- volatile关键字
- 通讯聊天
- 使用soap开发java-web--axis开发方案
- 过了《软件设计师》
- 启动Jboss的小小窍门
- TINYMCE 使用说明
- Ant入门教程
- 全套人民币
- 常用网站
- 全国高校BBS上最经典的语录