C#实现的Socket服务器端、客户端代码分享
作者:junjie 发布时间:2021-09-29 00:22:28
标签:C#,Socket,服务器端,客户端
服务端:
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Text;
namespace Server
{
class Program
{
static void Main(string[] args)
{
Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint point = new IPEndPoint(IPAddress.Parse("127.0.0.1"),55555);
try
{
server.Bind(point);
server.Listen(10);
//监听本地端口
System.Console.WriteLine("开始监听本地端口:55555");
while (true)
{
Socket sock = server.Accept();
byte[] buffer = new byte[1024 * 1024];
int n = sock.Receive(buffer);
String cmd = Encoding.UTF8.GetString(buffer, 0, n);
String result = execCmd(cmd);
byte[] bytes = Encoding.UTF8.GetBytes(result);
sock.Send(bytes);
}
}
catch (Exception ex)
{
System.Console.WriteLine(ex.Message);
return;
}
}
//重定向输入输出流,并且用cmd执行命令
private static String execCmd(String cmd)
{
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo = new System.Diagnostics.ProcessStartInfo();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments ="/c "+cmd;
//隐藏程序外壳
p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
//在这里重定向输出即可,因为不是交互式的,如果需要交互式的直接反弹cmd即可
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;
p.Start();
return p.StandardOutput.ReadToEnd();
}
}
}
客户端:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Windows.Forms;
/*
* Code By iswin
*/
namespace Client
{
public partial class main : Form
{
public main()
{
InitializeComponent();
this.ip.Text="127.0.0.1";
this.cmd.Text="ipconfig";
this.port.Text = "55555";
}
private void send_Click(object sender, EventArgs e)
{
Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
String remoteip=this.ip.Text;
String command = this.cmd.Text;
IPAddress ip = IPAddress.Parse(remoteip);
IPEndPoint point=new IPEndPoint(ip,int.Parse(port.Text));
try
{
this.recvmsg.Text = "开始连接服务端:" + remoteip + ":" + port.Text + "\n";
client.Connect(point);
this.recvmsg.Text="连接服务端!\n给服务端发送指令:"+command;
byte[] buffer = Encoding.UTF8.GetBytes(command);
//讲输入的指令发送到服务端
client.Send(buffer);
//接受服务端返回的数据
recvmsgs(client);
client.Close();
}
catch (Exception ex)
{
this.recvmsg.Text = ex.Message;
MessageBox.Show(ex.Message);
return;
}
}
//接受服务端发送来的消息
private void recvmsgs(Socket client)
{
try
{
byte[] buffer = new byte[1024 * 1024];
int size = client.Receive(buffer);
String recv = Encoding.UTF8.GetString(buffer, 0, size);
this.recvmsg.Text = "\n" + recv;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return;
}
}
}
}


猜你喜欢
- GUI编程实现贪吃蛇小游戏,供大家参考,具体内容如下1、编写主方法实现启动类2、准备好素材图片,编写数据类3、代码主体部分:在panel面板
- 成为一个优秀的Java程序员,有着良好的代码编写习惯是必不可少的。下面就让我们来看看代码编写的30条建议吧。(1) 类名首字母应该大写。字段
- 1.map遍历快速实现边距,文字自适应改变大小Container( // padding: EdgeI
- 1.View的坐标参数 主要有哪些?分别有什么注意的要点?答:Left,Right,top,Bottom 注意这4个值其实就是 view 和
- 简评:Android Support Library 26 中终于实现了一个等待已久的功能: RecyclerView 的快速滚动 。And
- 本文实例讲述了JFreeChart插件实现的折线图效果。分享给大家供大家参考,具体如下:package com.lei.jfreechart
- 我有以下课程public class ModInfo : IEquatable<ModInfo>{ public int ID
- 前言项目使用了SpringBoot构建项目。下面对动态调整日志的级别进行记录。从版本 1.5.1 之后就提供了基于 spring-boot-
- 目录一、泛型类型二、为什么需要泛型三、类型擦除四、类型擦除的后遗症五、Kotlin 泛型六、上界约束七、类型通配符 & 星号投影八、
- 背景:写一个用户登录拦截,在网上找了一圈没找到好用的,于是自己试验了一下,总结出来,分享给大家。1.自定义登录 * LoginInterce
- Mybatis Plus select 查询部分字段Mybatis Plus select语句默认查询所有字段,如需要指定字段查询,则需使用
- 前言Kotlin越来越流行,在Google的推动下发展的很迅猛,现在的项目大多使用上了Kotlin,其简练的语法糖确实能减少不少代码。Ada
- 先来简单说一下本文所要实现的功能:用户在浏览网页的时候,长按某一区域,识别如果是图片,则弹出弹框,出现保存图片的功能。同时识别图片是否是二维
- 1. 概述官方JavaDocsApi: javax.swing.JTextAreaJTextArea,文本区域。JTextArea 用来编辑
- 方法一、使用系统控件ViewFlipper方式:布局文件:<ViewFlipper andro
- 这几天做项目,有些地方的图片需要用到圆形图片,所以百度了一下,在github上找到一个开源项目,处理很简单,效果如下: 使用起来特
- 先看一下效果图实现思路:变成点的控件不是TextView和EditText而是Imageview。首先写一个RelativeLayout里边
- 在使用他人代码时,为不保留文件头部版权信息,需要一个个删掉,费时费力,写了个脚本,简单清除掉目录下所有的文件的头部版权信息。# -*- co
- 在并发多线程的情况下,为了保证数据安全性,一般我们会对数据进行加锁,通常使用Synchronized或者ReentrantLock同步锁。S
- 有时候在单机部署,或者项目没有在IDea 开发工具中运行(idea可以自动打开tomcat项目),需要项目启动后自动打开浏览器访问项目,配置