客户端和服务器程序 [英] Client and Server Program

查看:171
本文介绍了客户端和服务器程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

向所有编程专家致敬
我是网络编程的资深人士,我编写了一个简单的程序,用于在c#中创建客户端和服务器.cleint发送msg,服务器将其显示在列表框中( lbconnections)
问题是----
此应用程序在部署后可以正常运行,之后当我再次使用它时会崩溃(端口已被bieng用过的错误).plz请帮助我解决这个plzzzzzzzzzzzzzzzzzzzzzzzzzzzz.. ............ [困惑] [困惑] [困惑]

*********************** *********服务器编码****************************************** *****
使用System;
使用System.Threading;
使用System.Collections.Generic;
使用System.ComponentModel;
使用System.Data;
使用System .Drawing;
使用System.Text;
使用System.Windows.Forms;
使用System.Net.Sockets;
使用System.Net;

命名空间服务器
{
公共局部类Form1:Form
{
公共Form1()
{
InitializeComponent( );
}
public void recieve()
{
try
{
UdpClient服务器=新的UdpClient(8080);
while(true)
{
IPEndPoint ip =新IPEndPoint(IPAddress.Any,0);
收到的byte [] = server.Receive(ref ip);
字符串returndata = Encoding.ASCII.GetString(recieved);
listBox1.Items.Add(ip.Address.ToString()+:" + returndata.ToString());

}
}
捕获(SocketException ex)
{
MessageBox.Show(ex.Message);
}
}

私有void Form1_Load(对象发送方,EventArgs e)
{
线程服务器=新线程( new ThreadStart(receve));
server.Start();
}
}
}

**************** ***********************为客户编码********************** ***********************
使用System;
使用System.Collections.Generic;
使用System.ComponentModel;
使用System.Data;使用System.Drawing;使用System.Text;使用System.Windows.Forms;使用System.Net.Sockets;使用System.Net; br>
命名空间ser_cli
{
公共局部类Form1:Form
{
公共Form1()
{
InitializeComponent();
}

private void button1_Click(对象发送者,EventArgs e)
{
UdpClient客户端=新的UdpClient();
client.Connect(tbHost.Text,8080);
字节[] send = Encoding.ASCII.GetBytes("HELLO WORLD?");
client.Send(发送,send.Length);

}
}
}

A hi to all programming gurus
i am an amature in network programming i have written an simple program for making a client and server in c#
the cleint sends a msg and the server displays it in the listbox(lbconnections)
The Problem is that----
this application runs perfectly after being deployed and afterward when i again use it it crashes (port already bieng used error)
plz help me with this plzzzzzzzzzzzzzzzzzzzzzzzz..............[confused][confused][confused]

********************************coding for server*******************************************
using System;
using System.Threading;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Net;

namespace server
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void recieve()
{
try
{
UdpClient server = new UdpClient(8080);
while (true)
{
IPEndPoint ip = new IPEndPoint(IPAddress.Any, 0);
byte[] recieved = server.Receive(ref ip);
string returndata = Encoding.ASCII.GetString(recieved);
listBox1.Items.Add(ip.Address.ToString() + " : " + returndata.ToString());

}
}
catch (SocketException ex)
{
MessageBox.Show(ex.Message);
}
}

private void Form1_Load(object sender, EventArgs e)
{
Thread server = new Thread(new ThreadStart(recieve));
server.Start();
}
}
}

***************************************CODING FOR CLIENT*********************************************
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Net;

namespace ser_cli
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
UdpClient client = new UdpClient();
client.Connect(tbHost.Text, 8080);
byte[] send = Encoding.ASCII.GetBytes("HELLO WORLD?");
client.Send(send, send.Length);

}
}
}

推荐答案

我是C#的新手,所以我一直在浏览MSDN库,以了解如何使用某些类.阅读您的文章后,我寻找并找到了UdpClient类的描述.

http://msdn.microsoft.com/zh-cn/library/system.net.sockets.udpclient.aspx

该站点上有一些示例,但是具体来说,您需要关闭服务器连接,就像其他人告诉您的那样.因此,在用户关闭程序之前的某个地方,您必须调用server.Close()

我建议在您的receive()方法中使用finally块,然后在该处关闭服务器.但是您必须在try块之前定义服务器才能使它起作用.
I am new to C# so I''ve been browsing the MSDN libraries to see how to use certain classes. After reading your post, I looked for and found the description of the UdpClient class.

http://msdn.microsoft.com/en-us/library/system.net.sockets.udpclient.aspx

There are examples on that site, but specifically you need to close your server connection, as the others were telling you. So, somewhere before the user closes the program, you have to call server.Close()

I would recommend a finally block in your receive() method and closing the server there. But you would have to define server before the try block for this to work.


这篇关于客户端和服务器程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆