NetWork网络_TCP_双向输出聊天
来源:互联网 发布:centos安装wordpress 编辑:程序博客网 时间:2024/06/03 17:31
NetWork网络_TCP_双向输出聊天<13/9/2017>
服务器:
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Net.Sockets;using System.Net;using System.IO;using System.Threading;namespace 服务器{ class Program//服务器 { static TcpClient client; static void Main(string[] args) { //1.初始化 TcpListener server = new TcpListener(IPAddress.Parse("127.0.0.1"),8888); //localhost--本机ip server.Start(); //2.等待连接--阻塞式的 client = server.AcceptTcpClient(); Thread r = new Thread(Read); r.Start(); Thread w = new Thread(Write); w.Start(); r.Join(); w.Join(); } public static void Read() { NetworkStream ns = client.GetStream(); StreamReader sr = new StreamReader(ns); while (true) { //Thread.Sleep(500); Console.WriteLine(sr.ReadLine()); } } public static void Write() { StreamWriter sw = new StreamWriter(client.GetStream()); while (true) { //Thread.Sleep(500); string str = Console.ReadLine(); sw.WriteLine(str); sw.Flush(); } } }}客户端:
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Net.Sockets;using System.Net;using System.IO;using System.Threading;namespace 客户端{ class Program//客户端 { static TcpClient client; static void Main(string[] args) { client = new TcpClient("127.0.0.1", 8888); Thread r = new Thread(Read); r.Start(); Thread w = new Thread(Write); w.Start(); r.Join(); w.Join(); } public static void Read() { NetworkStream ns = client.GetStream(); StreamReader sr = new StreamReader(ns); while (true) { //Thread.Sleep(500); Console.WriteLine(sr.ReadLine()); } } public static void Write() { StreamWriter sw = new StreamWriter(client.GetStream()); while (true) { //Thread.Sleep(500); string str = Console.ReadLine(); sw.WriteLine(str); sw.Flush(); } } }}先启动服务器再启动客户端(服务器实际上也是一个客户端),IP地址查询cmd然后Ipconfig,服务器和客户端保持同一网络,同一IP地址,最终实现简易聊天功能,如下:
阅读全文
0 0
- NetWork网络_TCP_双向输出聊天
- NetWork网络_TCP_在unity中初步实现局域网互联效果
- JavaSE_网络编程_TCP_练习1
- Unity网络通信(二)Unity Network 聊天
- Unity网络通信(二)Unity Network 聊天
- Unity网络通信(二)Unity Network 聊天
- JavaSE_网络编程_TCP_练习2_文本文件
- JavaSE_网络编程_TCP_练习3_图片
- [Unity3d] Network 聊天
- network 多线程聊天
- JavaSE_网络编程_TCP_练习4_与浏览器和服务端交互
- 利用TCP实现双向聊天
- 网络 网络层(Network)
- 观察者模式、简单一对一双向聊天系统
- 用TCP实现双向聊天系统
- 利用UDP实现双向聊天系统
- UDP实现的简易双向聊天功能
- 10.10网络聊天
- 142. Linked List Cycle II
- Spring JTA多数据源事务管理详细教程
- Spring Boot-配置文件详解:Properties和YAML
- LeetCode:M-200. Number of Islands
- CentOS6.5 lv调整空间大小
- NetWork网络_TCP_双向输出聊天
- neural-style、chainer-fast-neuralstyle图像风格转换使用
- 数字三角形
- python中from lxml import etree,在使用第三方包lxml引入etree模块时报错:
- PM、GAM、infoGAN等框架
- redis的高级事务CAS(乐观锁)
- 微信浏览器缓存原因导致点击时间失效
- 【opencv学习之十】opencv3和opencv2读取本地摄像头
- php调用C编写的类库方法