客户端
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
namespace SynchronousSocketClient
{
class Program
{
public static string strNm = null;
public static void StartClient( string s, bool fg)
{
// Data buffer for incoming data.
byte[] bytes = new byte[1024];
// Connect to a remote device.
try
{
// Establish the remote endpoint for the socket.
// This example uses port 11000 on the local computer.
//IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());
//IPAddress ipAddress = ipHostInfo.AddressList[1];
IPAddress ipAddress = IPAddress.Parse("192.168.1.128");
IPEndPoint remoteEP = new IPEndPoint(ipAddress, 11000);
// Create a TCP/IP socket.
Socket sender = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
// Connect the socket to the remote endpoint. Catch any errors.
try
{
sender.Connect(remoteEP);
//Console.WriteLine("Socket connected to {0}",
sender.RemoteEndPoint.ToString();
// Encode the data string into a byte array.
byte[] msg = Encoding.ASCII.GetBytes(s);
// Send the data through the socket.
int bytesSent = sender.Send(msg);
// Receive the response from the remote device.
while (true)
{
int bytesRec = bytesRec = sender.Receive(bytes);
string recvdata = Encoding.ASCII.GetString(bytes, 0, bytesRec);
//Console.WriteLine("Echoed wwwwwwwwwww = {0}", recvdata);
if (recvdata == "" || fg)
{
break;
}
Console.WriteLine("{0}", recvdata);
}
// Release the socket.
sender.Shutdown(SocketShutdown.Both);
sender.Close();
}
catch (ArgumentNullException ane)
{
Console.WriteLine("ArgumentNullException : {0}", ane.ToString());
}
catch (SocketException se)
{
Console.WriteLine("SocketException : {0}", se.ToString());
}
catch (Exception e)
{
Console.WriteLine("Unexpected exception : {0}", e.ToString());
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
static void Main(string[] args)
{
while (strNm == null || strNm == " say : \n ")
{
Console.WriteLine("Please your username ...");
strNm = Console.ReadLine() + " say : \n ";
}
while (true)
{
bool flg = true;
Console.WriteLine("Please WriteLine...");
string sflg = Console.ReadLine();
if (sflg == "over")
{
flg = false;
}
StartClient(strNm + sflg + "<EOF>", flg);
}
}
}
}
服务器端
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
namespace SynchronousSocketClient
{
class Program
{
public static string strNm = null;
public static void StartClient( string s, bool fg)
{
// Data buffer for incoming data.
byte[] bytes = new byte[1024];
// Connect to a remote device.
try
{
// Establish the remote endpoint for the socket.
// This example uses port 11000 on the local computer.
//IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());
//IPAddress ipAddress = ipHostInfo.AddressList[1];
IPAddress ipAddress = IPAddress.Parse("192.168.1.128");
IPEndPoint remoteEP = new IPEndPoint(ipAddress, 11000);
// Create a TCP/IP socket.
Socket sender = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
// Connect the socket to the remote endpoint. Catch any errors.
try
{
sender.Connect(remoteEP);
//Console.WriteLine("Socket connected to {0}",
sender.RemoteEndPoint.ToString();
// Encode the data string into a byte array.
byte[] msg = Encoding.ASCII.GetBytes(s);
// Send the data through the socket.
int bytesSent = sender.Send(msg);
// Receive the response from the remote device.
while (true)
{
int bytesRec = bytesRec = sender.Receive(bytes);
string recvdata = Encoding.ASCII.GetString(bytes, 0, bytesRec);
//Console.WriteLine("Echoed wwwwwwwwwww = {0}", recvdata);
if (recvdata == "" || fg)
{
break;
}
Console.WriteLine("{0}", recvdata);
}
// Release the socket.
sender.Shutdown(SocketShutdown.Both);
sender.Close();
}
catch (ArgumentNullException ane)
{
Console.WriteLine("ArgumentNullException : {0}", ane.ToString());
}
catch (SocketException se)
{
Console.WriteLine("SocketException : {0}", se.ToString());
}
catch (Exception e)
{
Console.WriteLine("Unexpected exception : {0}", e.ToString());
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
static void Main(string[] args)
{
while (strNm == null || strNm == " say : \n ")
{
Console.WriteLine("Please your username ...");
strNm = Console.ReadLine() + " say : \n ";
}
while (true)
{
bool flg = true;
Console.WriteLine("Please WriteLine...");
string sflg = Console.ReadLine();
if (sflg == "over")
{
flg = false;
}
StartClient(strNm + sflg + "<EOF>", flg);
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
两种局域网聊天源码
共1个文件
txt:1个
需积分: 0 32 下载量 19 浏览量
2008-03-16
20:04:03
上传
评论
收藏 3KB RAR 举报
温馨提示
控制台方式 与加密CHAT方式
资源详情
资源评论
资源推荐
收起资源包目录
聊天.rar (1个子文件)
聊天.txt 15KB
共 1 条
- 1
xuefanbiao
- 粉丝: 0
- 资源: 8
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0