关于LINUX下 socket的 TCPClient调试问题!

关于LINUX下 socket的 TCPClient调试问题!,第1张

1. 代码中有提示啊,是两个参数,argc是包括执行程序名称的

./TCPClient <碧悔橘ServerIP><ServerPort>

第一个参数是服务器的IP地址,第二个参数是服务器开放的端口号

2. 是的,对于TCP Socket来说,必须先connect,成功后即可发送/接收。

需要注意的是,TCP是全双工前哗的,可以同时发送/接收,而不是lz说的先send再recv。

具体如何,取决于实际的应用,如果你的应用中服务器必须先等待客户端发送数据,然后服务器作出回应,那就是先send再recv

但TCP协议是没有这样的约悔团束的。

服务端:

class mTcpServer

{

class server

{

/// <summary>

/// 应用程序的雀咐主入口点。

/// </summary>

[STAThread]

static void Main(string[] args)

{

Thread newThread

try

{

serverTcpListener = new TcpListener(localAddr, port)

serverTcpListener.Start()

while (true)

{

Console.Write("Waiting for a connection... ")

client = serverTcpListener.AcceptTcpClient()

Console.WriteLine("Connected!")

newThread = new Thread(new ThreadStart(newScream))

newThread.Start()

}

}

catch (SocketException e)

{

Console.WriteLine("SocketException: {0}", e)

}

finally

{

// Stop listening for new clients.

Console.WriteLine("Stop listening for new clients")

serverTcpListener.Stop()

}

Console.WriteLine("\nHit enter to continue...")

Console.Read()

}

static Int32 port = 9050

static IPAddress localAddr = IPAddress.Parse("127.0.0.1"掘桐)

static TcpListener serverTcpListener = null

static Byte[] bytes = new Byte[256]

static String data = null

static TcpClient client

static void newScream()

{

data = null

NetworkStream stream = client.GetStream()

stream.Write(Encoding.ASCII.GetBytes("判岁坦Connected!"), 0, 10)

int i

while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)

{

data = System.Text.Encoding.ASCII.GetString(bytes, 0, i)

Console.WriteLine("Received: {0}", data)

data = data.ToUpper()

byte[] msg = System.Text.Encoding.ASCII.GetBytes(data)

stream.Write(msg, 0, msg.Length)

Console.WriteLine("Sent: {0}", data)

}

}

}

}

客户端:

using System

using System.Net

using System.Net.Sockets

using System.Text

namespace mTcpClint

{

/// <summary>

/// Class1 的摘要说明。

/// </summary>

class client

{

/// <summary>

/// 应用程序的主入口点。

/// </summary>

[STAThread]

static void Main(string[] args)

{

//

// TODO: 在此处添加代码以启动应用程序

//

byte[] data = new byte[1024]

//Socket newclient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)

Console.Write("please input the server ip:")

string ipadd = Console.ReadLine()

Console.WriteLine()

Console.Write("please input the server port:")

int port = Convert.ToInt32(Console.ReadLine())

IPEndPoint ie = new IPEndPoint(IPAddress.Parse(ipadd), port)//服务器的IP和端口

TcpClient newclient

newclient = new TcpClient()

newclient.Connect(ipadd, port)

int recv = newclient.Client.Receive(data)

string stringdata = Encoding.ASCII.GetString(data, 0, recv)

Console.WriteLine(stringdata)

while (true)

{

string input = Console.ReadLine()

if (input == "exit") break

newclient.Client.Send(Encoding.ASCII.GetBytes(input))

data = new byte[1024]

recv = newclient.Client.Receive(data)

stringdata = Encoding.ASCII.GetString(data, 0, recv)

Console.WriteLine(stringdata)

}

Console.WriteLine("disconnect from server")

newclient.Client.Shutdown(SocketShutdown.Both)

newclient.Close()

}

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/12544977.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-26
下一篇 2023-05-26

发表评论

登录后才能评论

评论列表(0条)

保存