server,服务器代码。
使用socket套接字连接。
using system;
using system.net;
using system.net.sockets;
using system.io ;
public class echoserver
{
//entry point of main method.
public static void main()
{
//tcplistener is listening on the given port
int32 port = 1234;
//ipaddress is connetct ip address
//ipaddress addr = ipaddress.parse("127.0.0.1");
ipaddress ipaddress = dns.resolve("localhost").addresslist[0];
tcplistener tcplistener = new tcplistener(ipaddress,port);
tcplistener.start();
console.writeline("server started") ;
//accepts a new connection
socket socketforclient = tcplistener.acceptsocket();
//streamwriter and streamreader classes for reading and writing the data to and from.
//the server reads the meassage sent by the client ,converts it to upper case and sends it back to the client.
//lastly close all the streams.
try
{
if (socketforclient.connected)
{
while(true)
{
console.writeline("client connected");
networkstream networkstream = new networkstream(socketforclient);
streamwriter streamwriter = new streamwriter(networkstream);
streamreader streamreader = new streamreader(networkstream);
string line = streamreader.readline();
console.writeline("read:" +line);
line=line.toupper()+ "!";
streamwriter.writeline(line);
console.writeline("wrote:"+line);
streamwriter.flush() ;
}
}
socketforclient.close();
console.writeline("exiting");
}
catch(exception e)
{
console.writeline(e.tostring()) ;
}
}
}
client,客户端程序,在文本框中输入字符,将在列表框显示。
using system;
using system.text;
using system.drawing;
using system.collections;
using system.componentmodel;
using system.windows.forms;
using system.net;
using system.net.sockets;
using system.io;
namespace socketsample
{
public class sample : system.windows.forms.form
{
private system.windows.forms.button bts;
private system.windows.forms.textbox t1;
private networkstream networkstream ;
private streamreader streamreader ;
private streamwriter streamwriter ;
arraylist sb;
tcpclient myclient;
bool flag=false;
private system.windows.forms.listbox t2;
private system.componentmodel.container components = null;
public sample()
{
sb = new arraylist();
initializecomponent();
if(!flag)
connect();
//get a network stream from the server
networkstream = myclient.getstream();
streamreader = new streamreader(networkstream);
streamwriter = new streamwriter(networkstream);
showmessage();
}
protected override void dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.dispose();
}
}
base.dispose( disposing );
}
windows 窗体设计器生成的代码#region windows 窗体设计器生成的代码
/**////
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
///
private void initializecomponent()
{
system.resources.resourcemanager resources = new system.resources.resourcemanager(typeof(sample));
this.t1 = new system.windows.forms.textbox();
this.bts = new system.windows.forms.button();
this.t2 = new system.windows.forms.listbox();
this.suspendlayout();
//
// t1
//
this.t1.location = new system.drawing.point(24, 32);
this.t1.name = "t1";
this.t1.size = new system.drawing.size(280, 21);
this.t1.tabindex = 0;
this.t1.text = "";
this.t1.textchanged += new system.eventhandler(this.t1_textchanged);
//
// bts
//
this.bts.backgroundimage = ((system.drawing.image)(resources.getobject("bts.backgroundimage")));
this.bts.enabled = false;
this.bts.flatstyle = system.windows.forms.flatstyle.popup;
this.bts.location = new system.drawing.point(320, 32);
this.bts.name = "bts";
this.bts.tabindex = 1;
this.bts.text = "send";
this.bts.click += new system.eventhandler(this.bts_click);
//
// t2
//
this.t2.borderstyle = system.windows.forms.borderstyle.fixedsingle;
this.t2.font = new system.drawing.font("courier new", 9f, system.drawing.fontstyle.regular, system.drawing.graphicsunit.point, ((system.byte)(0)));
this.t2.itemheight = 15;
this.t2.location = new system.drawing.point(24, 64);
this.t2.name = "t2";
this.t2.size = new system.drawing.size(368, 212);
this.t2.tabindex = 2;
//
// sample
//
this.autoscalebasesize = new system.drawing.size(6, 14);
this.backgroundimage = ((system.drawing.image)(resources.getobject("$this.backgroundimage")));
this.clientsize = new system.drawing.size(416, 297);
this.controls.add(this.t2);
this.controls.add(this.bts);
this.controls.add(this.t1);
this.name = "sample";
this.text = "sample";
this.resumelayout(false);
}
#endregion
public static void main()
{
sample df=new sample();
df.formborderstyle=formborderstyle.fixed3d;
application.run(df);
}
protected void connect()
{
//connect to the "localhost" at the give port
//if you have some other server name then you can use that instead of "localhost"
try
{
sb.add("conneting to server");
myclient = new tcpclient("localhost", 1234);
sb.add("conneted,please enter something in the textbox");
}
catch
{
sb.add(string.format("failed to connect to server at {0}:1234", "localhost"));
}
flag = true;
}
protected void showmessage()
{
for(int i=0;i
t2.items.add((object)sb[i].tostring());
}
sb.clear();
}
private void t1_textchanged(object sender, system.eventargs e)
{
if(t1.text == "" )
bts.enabled = false;
else
bts.enabled=true;
}
private void bts_click(object sender, system.eventargs e)
{
if(t1.text=="")
{
sb.add( "please enter something in the textbox.");
t1.focus();
return ;
}
string s;
try
{
streamwriter.writeline(t1.text);
console.writeline("sending message");
streamwriter.flush();
s= streamreader.readline();
console.writeline("reading message") ;
console.writeline(s) ;
sb.add(s);
t1.text = "";
t1.focus();
showmessage();
}
catch
{
messagebox.show("error.");
}
}
}
}
新闻热点
疑难解答