聊天室可以相互交流
2015-09-24 22:12
501 查看
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace 聊天室服务端2
{
class Program
{
List<Socket> socketList = new List<Socket>();
static void Main(string[] args)
{
Socket mainServer = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
int pose = 3001;
IPAddress ip = IPAddress.Parse("192.168.1.104");
EndPoint point = new IPEndPoint(ip, pose);
mainServer.Bind(point);
mainServer.Listen(10);
Console.WriteLine("服务器启动成功,等待连接...");
Socket ClientVIP = mainServer.Accept();
Console.WriteLine("连接成功");
IPEndPoint ClientVIPip = (IPEndPoint)ClientVIP.RemoteEndPoint;
Console.WriteLine("客户端地址{0},端口{1}", ClientVIPip.Address, ClientVIPip.Port);
ReceiveAndSend RS = new ReceiveAndSend(ClientVIP);
RS.Se();
RS.Re();
Console.ReadKey();
}
}
class ReceiveAndSend
{
private Socket socketObj;
private Thread tse,tre;
public ReceiveAndSend(Socket s)
{
socketObj = s;
tse = new Thread(Se);
tse.Start();
tre = new Thread(Re);
tre.Start();
}
public void Se()
{
while (true)
{
string sendStr = "服务器:"+Console.ReadLine();
byte[] sendData = Encoding.Default.GetBytes(sendStr);
socketObj.Send(sendData);//发送
}
}
public void Re()
{
while (true)
{
byte[] recData = new byte[1024];
int length = socketObj.Receive(recData);//读取
string recStr = Encoding.Default.GetString(recData, 0, length);
Console.WriteLine(recStr);
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace 聊天室客户端2
{
class Program
{
static void Main(string[] args)
{
Socket mainClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
int pose = 3001;
IPAddress ip = IPAddress.Parse("192.168.1.104");
EndPoint point = new IPEndPoint(ip, pose);
mainClient.Connect(point);
ReceiveAndSend RS = new ReceiveAndSend(mainClient);
RS.Re();
RS.Se();
Console.ReadKey();
}
}
class ReceiveAndSend
{
private Socket socketObj;
private Thread tse, tre;
public ReceiveAndSend(Socket s)
{
socketObj = s;
tse = new Thread(Se);
tse.Start();
tre = new Thread(Re);
tre.Start();
}
public void Se()
{
while (true)
{
string sendStr = "客户端:" + Console.ReadLine();
byte[] sendData = Encoding.Default.GetBytes(sendStr);
socketObj.Send(sendData);//发送
}
}
public void Re()
{
while (true)
{
byte[] recData = new byte[1024];
int length = socketObj.Receive(recData);//读取
string recStr = Encoding.Default.GetString(recData, 0, length);
Console.WriteLine(recStr);
}
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace 聊天室服务端2
{
class Program
{
List<Socket> socketList = new List<Socket>();
static void Main(string[] args)
{
Socket mainServer = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
int pose = 3001;
IPAddress ip = IPAddress.Parse("192.168.1.104");
EndPoint point = new IPEndPoint(ip, pose);
mainServer.Bind(point);
mainServer.Listen(10);
Console.WriteLine("服务器启动成功,等待连接...");
Socket ClientVIP = mainServer.Accept();
Console.WriteLine("连接成功");
IPEndPoint ClientVIPip = (IPEndPoint)ClientVIP.RemoteEndPoint;
Console.WriteLine("客户端地址{0},端口{1}", ClientVIPip.Address, ClientVIPip.Port);
ReceiveAndSend RS = new ReceiveAndSend(ClientVIP);
RS.Se();
RS.Re();
Console.ReadKey();
}
}
class ReceiveAndSend
{
private Socket socketObj;
private Thread tse,tre;
public ReceiveAndSend(Socket s)
{
socketObj = s;
tse = new Thread(Se);
tse.Start();
tre = new Thread(Re);
tre.Start();
}
public void Se()
{
while (true)
{
string sendStr = "服务器:"+Console.ReadLine();
byte[] sendData = Encoding.Default.GetBytes(sendStr);
socketObj.Send(sendData);//发送
}
}
public void Re()
{
while (true)
{
byte[] recData = new byte[1024];
int length = socketObj.Receive(recData);//读取
string recStr = Encoding.Default.GetString(recData, 0, length);
Console.WriteLine(recStr);
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace 聊天室客户端2
{
class Program
{
static void Main(string[] args)
{
Socket mainClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
int pose = 3001;
IPAddress ip = IPAddress.Parse("192.168.1.104");
EndPoint point = new IPEndPoint(ip, pose);
mainClient.Connect(point);
ReceiveAndSend RS = new ReceiveAndSend(mainClient);
RS.Re();
RS.Se();
Console.ReadKey();
}
}
class ReceiveAndSend
{
private Socket socketObj;
private Thread tse, tre;
public ReceiveAndSend(Socket s)
{
socketObj = s;
tse = new Thread(Se);
tse.Start();
tre = new Thread(Re);
tre.Start();
}
public void Se()
{
while (true)
{
string sendStr = "客户端:" + Console.ReadLine();
byte[] sendData = Encoding.Default.GetBytes(sendStr);
socketObj.Send(sendData);//发送
}
}
public void Re()
{
while (true)
{
byte[] recData = new byte[1024];
int length = socketObj.Receive(recData);//读取
string recStr = Encoding.Default.GetString(recData, 0, length);
Console.WriteLine(recStr);
}
}
}
}
相关文章推荐
- MySQL日期字符串转换成NULL值的异常处理
- 专利申请--权利要求书vs说明书
- android基础知识
- android中dp(dip) px sp
- lintcode-线段树查询II-247
- 纯虚函数
- 转载老师的一篇文章——java自学之路
- CGI,fastcgi和php-fpm
- CGI,fastcgi和php-fpm
- margin和padding的区别
- shell脚本学习 一
- ZOJ 2369 | SGU 217 | Acdream 1234 自适应辛普森积分
- NYOJ 837 Wythoff Game(威佐夫博奕公式利用)
- USB host 通信
- 一个关于java数据库驱动包的小发现
- 2015年9月20日第一节课html2-2
- js中return、return true、return false的区别
- asp.net中SQL语句太长,怎么换行写?
- ubuntu 安装numpy和matplotlib
- 手游发展迅猛,渠道要搭建移动游戏生态圈才有出路