纳金网
标题:
Socket服务端 与客户端聊天 实现
[打印本页]
作者:
烟雨
时间:
2015-5-28 01:35
标题:
Socket服务端 与客户端聊天 实现
服务器端:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("server:192.168.104.98");//192.168.104.98是我自己主机的ip地址
//创建服务器socket实例 基于Tcp 以字节流传输*
Socket socket_sever = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPAddress[] ipds = Dns.GetHostAddresses(Dns.GetHostName());
//给定服务器Ip和端口
IPEndPoint ipEndPoint = new IPEndPoint(ipds[1], 2222);
//绑定ip地址和端口号
socket_sever.Bind(ipEndPoint);
//开始监听 *
socket_sever.Listen(40);
while (true)
{
byte[] data = new byte[1024];//用于缓存客户端发来的数据,以字节数组存储
Console.WriteLine("等待客户端连接");
//连接*
Socket client = socket_sever.Accept();//开启一个新的 socket用于和客户端通信
IPEndPoint clientPoint = (IPEndPoint)client.RemoteEndPoint;//获取到客户端的ip 和端口
Console.WriteLine("connect with client:" + clientPoint.Address + "port" + clientPoint.Port);
string server_msg = "welcome here";
data = Encoding.ASCII.GetBytes(server_msg); //将字符串转化为字节数组
client.Send(data, data.Length, SocketFlags.None);
while (true)
{
//使用循环获取客户端的数据
//收消息*
data = new byte[1024];
string client_meg = "";//记录传输客户端传过来的数据
int lenght = client.Receive(data);//把客户端发过来的数据读出来
client_meg = Encoding.ASCII.GetString(data, 0, lenght);
if (client_meg == "exit")
{
break;
}
Console.WriteLine("client_mg:" + client_meg);
//发消息
Console.WriteLine("请示入:");
server_msg = Console.ReadLine();
data = Encoding.ASCII.GetBytes(server_msg);
client.Send(data, data.Length, SocketFlags.None);//将消息发给客户端
}
Console.WriteLine(clientPoint.Address + "已经断开连接");
client.Close();//关闭连接
}
socket_sever.Close();//关闭socket服务
}
}
}
复制代码
客户端:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
//创建一个socket实例*
Socket socket_client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
//指定服务器ip
string ipStr = "192.168.105.240";
IPAddress ipaddress = IPAddress.Parse(ipStr);//将字符串转为ip地址
IPEndPoint ipEndPoint = new IPEndPoint(ipaddress, 2222);//指定ip和端口号
//连接服务器*
socket_client.Connect(ipEndPoint);
byte[] data;
int lenght = 0;
string server_msg="";
while (true)
{
//接收
data = new byte[1024];
//会处于等待消息状态*
lenght = socket_client.Receive(data); ;//接受服务器的消息
server_msg = Encoding.ASCII.GetString(data, 0, lenght);//将数据流转化为字符串
Console.WriteLine("server:" + server_msg);
//发送
Console.WriteLine("请输入:");
string input = Console.ReadLine();
if (input=="exit")
{
socket_client.Send(Encoding.ASCII.GetBytes(input));
break;
}
//发送*
socket_client.Send(Encoding.ASCII.GetBytes(input));
}
Console .WriteLine ("已经断开连接");
socket_client.Shutdown(SocketShutdown.Both );//将连接断掉
socket_client .Close ();//关闭socket
}
}
}
复制代码
欢迎光临 纳金网 (http://go.narkii.com/club/)
Powered by Discuz! X2.5