求助网络编程

scegg 2003-06-29 06:35:07
您好。
我是一个学习VB.NET网络编程的新手,因为它与VB的WINSOCK差别太大了,一时无法适应,请教高手给予指点,写一个例题,谢谢。

题目要求:创建C/S程序,服务器端将用户输入的数据发送到客户端,客户端收到并显示。不能限制发送数据的文本长度,不能限制文本内容,不能限制文本的次数。使用UNICODE编码。将程序做成CONSOLO就可以了(随便)。

例如:SERVER端口,分别发送“啊”,“你好”,“123”三次数据到客户端,客户端在每次收到后回复相同的内容到服务器。

谢谢。
谢绝水帖。
...全文
44 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
scegg 2003-09-06
  • 打赏
  • 举报
回复
那也好啊,请帖吧,谢谢啦.
yufenfeila 2003-09-02
  • 打赏
  • 举报
回复
呵呵,他是抄的MSDN,但是感觉挺有用的

你要是感觉深刻,我有一个代码,但是不是CONSOLO,是有窗口的程序
scegg 2003-09-02
  • 打赏
  • 举报
回复
写太深看不懂。
scegg 2003-07-02
  • 打赏
  • 举报
回复
这个好象不是我要的东西吧?
Montaque 2003-06-29
  • 打赏
  • 举报
回复
下面的示例创建 TcpListener。

[Visual Basic]
Public Shared Sub Main()

Try
' Set the TcpListener on port 13000.
Dim port As Int32 = 13000
Dim localAddr As IPAddress = IPAddress.Parse("127.0.0.1")

Dim server As New TcpListener(localAddr, port)

' Start listening for client requests.
server.Start()

' Buffer for reading data
Dim bytes(1024) As [Byte]
Dim data As [String] = Nothing

' Enter the listening loop.
While True
Console.Write("Waiting for a connection... ")

' Perform a blocking call to accept requests.
' You could also user server.AcceptSocket() here.
Dim client As TcpClient = server.AcceptTcpClient()
Console.WriteLine("Connected!")

data = Nothing

' Get a stream object for reading and writing
Dim stream As NetworkStream = client.GetStream()

Dim i As Int32

' Loop to receive all the data sent by the client.
i = stream.Read(bytes, 0, bytes.Length)
While (i <> 0)
' Translate data bytes to a ASCII string.
data = System.Text.Encoding.ASCII.GetString(bytes, 0, i)
Console.WriteLine([String].Format("Received: {0}", data))

' Process the data sent by the client.
data = data.ToUpper()

Dim msg As [Byte]() = System.Text.Encoding.ASCII.GetBytes(data)

' Send back a response.
stream.Write(msg, 0, msg.Length)
Console.WriteLine([String].Format("Sent: {0}", data))

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

End While

' Shutdown and end connection
client.Close()
End While
Catch e As SocketException
Console.WriteLine("SocketException: {0}", e)
End Try

Console.WriteLine(ControlChars.Cr + "Hit enter to continue...")
Console.Read()
End Sub 'Main

[C#]
public static void Main()
{

try
{
// Set the TcpListener on port 13000.
Int32 port = 13000;
IPAddress localAddr = IPAddress.Parse("127.0.0.1");

// TcpListener server = new TcpListener(port);
TcpListener server = new TcpListener(localAddr, port);

// Start listening for client requests.
server.Start();

// Buffer for reading data
Byte[] bytes = new Byte[256];
String data = null;

// Enter the listening loop.
while(true)
{
Console.Write("Waiting for a connection... ");

// Perform a blocking call to accept requests.
// You could also user server.AcceptSocket() here.
TcpClient client = server.AcceptTcpClient();
Console.WriteLine("Connected!");

data = null;

// Get a stream object for reading and writing
NetworkStream stream = client.GetStream();

Int32 i;

// Loop to receive all the data sent by the client.
while((i = stream.Read(bytes, 0, bytes.Length))!=0)
{
// Translate data bytes to a ASCII string.
data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
Console.WriteLine(String.Format("Received: {0}", data));

// Process the data sent by the client.
data = data.ToUpper();

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

// Send back a response.
stream.Write(msg, 0, msg.Length);
Console.WriteLine(String.Format("Sent: {0}", data));
}

// Shutdown and end connection
client.Close();
}
}
catch(SocketException e)
{
Console.WriteLine("SocketException: {0}", e);
}

Console.WriteLine("\nHit enter to continue...");
Console.Read();
}

[C++]
void main() {
try {
// Set the TcpListener on port 13000.
Int32 port = 13000;
IPAddress* localAddr = IPAddress::Parse(S"127.0.0.1");

// TcpListener* server = new TcpListener(port);
TcpListener* server = new TcpListener(localAddr, port);

// Start listening for client requests.
server->Start();

// Buffer for reading data
Byte bytes[] = new Byte[256];
String* data = 0;

// Enter the listening loop.
while (true) {
Console::Write(S"Waiting for a connection... ");

// Perform a blocking call to accept requests.
// You could also user server.AcceptSocket() here.
TcpClient* client = server->AcceptTcpClient();
Console::WriteLine(S"Connected!");

data = 0;

// Get a stream Object* for reading and writing
NetworkStream* stream = client->GetStream();

Int32 i;

// Loop to receive all the data sent by the client.
while (i = stream->Read(bytes, 0, bytes->Length)) {
// Translate data bytes to a ASCII String*.
data = Text::Encoding::ASCII->GetString(bytes, 0, i);
Console::WriteLine(String::Format(S"Received: {0}", data));

// Process the data sent by the client.
data = data->ToUpper();

Byte msg[] = Text::Encoding::ASCII->GetBytes(data);

// Send back a response.
stream->Write(msg, 0, msg->Length);
Console::WriteLine(String::Format(S"Sent: {0}", data));
}

// Shutdown and end connection
client->Close();
}
} catch (SocketException* e) {
Console::WriteLine(S"SocketException: {0}", e);
}

Console::WriteLine(S"\nHit enter to continue...");
Console::Read();
}

[JScript] 没有可用于 JScript 的示例。若要查看 Visual Basic、C# 或 C++ 示例,请单击页左上角的“语言筛选器”按钮 。

要求
命名空间: System.Net.Sockets

Montaque 2003-06-29
  • 打赏
  • 举报
回复
Try
' Create a TcpClient.
' Note, for this client to work you need to have a TcpServer
' connected to the same address as specified by the server, port
' combination.
Dim port As Int32 = 13000
Dim client As New TcpClient(server, port)

' Translate the passed message into ASCII and store it as a Byte array.
Dim data As [Byte]() = System.Text.Encoding.ASCII.GetBytes(message)

' Get a client stream for reading and writing.
' Stream stream = client.GetStream();
Dim stream As NetworkStream = client.GetStream()

' Send the message to the connected TcpServer.
stream.Write(data, 0, data.Length)

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

' Receive the TcpServer.response.
' Buffer to store the response bytes.
data = New [Byte](256) {}

' String to store the response ASCII representation.
Dim responseData As [String] = [String].Empty

' Read the first batch of the TcpServer response bytes.
Dim bytes As Int32 = stream.Read(data, 0, data.Length)
responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes)
Console.WriteLine("Received: {0}", responseData)

' Close everything.
client.Close()
Catch e As ArgumentNullException
Console.WriteLine("ArgumentNullException: {0}", e)
Catch e As SocketException
Console.WriteLine("SocketException: {0}", e)
End Try

Console.WriteLine(ControlChars.Cr + " Press Enter to continue...")
Console.Read()
dotnba 2003-06-29
  • 打赏
  • 举报
回复
参与就是支持...
重在参与...

16,554

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧