免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2774 | 回复: 0

[推荐]Java C/S mode --Client [复制链接]

论坛徽章:
0
发表于 2002-11-12 18:05 |显示全部楼层
import java.applet.*&#59;
import java.awt.*&#59;
import java.awt.event.*&#59;
import java.io.*&#59;
import java.net.*&#59;

//public String str&#59;


class javaclient extends Panel implements ActionListener
{


TextField tf&#59;
LudCanvas cv&#59;
Button b&#59;

LudPanel(LudCanvas cv)
{
this.cv = cv&#59;

tf = new TextField("ludejun",30)&#59;
add(tf)&#59;

b = new Button("退出&quot&#59;
b.addActionListener(this)&#59;
add(b)&#59;
b = new Button("发送&quot&#59;
b.addActionListener(this)&#59;
add(b)&#59;
}


public str actionPerformed(ActionEvent ev)
{
String str = ev.getActionCommand()&#59;
if( str.equals("退出&quot )
{
//tf.setText("This is my first button&quot&#59;
System.exit(0)&#59;
}
else
{
str = tf.getText()&#59;
tf.setText("开始检测发送......&quot&#59;
return str&#59;

}
}






public static void main(String args[])
{
String data&#59;
Socket Client&#59;
DataInputStream InputS&#59;
DataInputStream KeyS&#59;
PrintStream OutputS&#59;

Basic h = new Basic()&#59;

int i = 0&#59;



try
{
Client = new Socket(args[0],600)&#59;
InputS = new
DataInputStream(Client.getInputStream())&#59;
OutputS = new PrintStream(Client.getOutputStream())&#59;
KeyS = new DataInputStream(System.in)&#59;
}
catch(IOException e)
{
System.out.println("Cannot Connect with Server&quot&#59;
return&#59;
}

try
{
while (i<5)
{
data = KeyS.readLine()&#59;
OutputS.println(data)&#59;
System.out.println(&quot;ECHO From Server:&quot;+ InputS.readLine())&#59;
i++&#59;
}
}
catch(IOException e)
{
System.out.println(&quot;IOException Happened&quot&#59;
}

try
{
System.out.println(&quot;Now will end this program&quot&#59;
Client.close()&#59;
}
catch(IOException e)
{
System.out.println(&quot;system cannot close socket&quot&#59;
}
}
}




//@@@@@@@@@@@@@@@@@@@@@@@222




class LudCanvas extends Canvas
{
public void paint(Graphics g)
{
Rectangle r = getBounds()&#59;
g.setColor(Color.red)&#59;
int panelHeight = 15&#59;
int b = 3&#59;
g.drawRect(b , b , r.width-b-b , r.height-b-b-panelHeight)&#59;
int t = 10&#59;
g.drawRect(t , t , r.width-t-t , r.height-t-t-panelHeight)&#59;

g.drawString(&quot;The first applet of mine&quot;, 120, 30)&#59;


Image img = Toolkit.getDefaultToolkit().getImage(&quot;ie.gif&quot&#59;

g.drawImage(img, 50, 50, this)&#59;


}

}



class LudPanel extends Panel implements ActionListener
{
TextField tf&#59;
LudCanvas cv&#59;
Button b&#59;

LudPanel(LudCanvas cv)
{
this.cv = cv&#59;

tf = new TextField(&quot;ludejun&quot;,30)&#59;
add(tf)&#59;

b = new Button(&quot;退出&quot;)&#59;
b.addActionListener(this)&#59;
add(b)&#59;
b = new Button(&quot;发送&quot;)&#59;
b.addActionListener(this)&#59;
add(b)&#59;
}


public void actionPerformed(ActionEvent ev)
{
String str = ev.getActionCommand()&#59;
if( str.equals(&quot;退出&quot;) )
{
//tf.setText(&quot;This is my first button&quot;)&#59;
System.exit(0)&#59;
}
else
{
tf.setText(&quot;开始检测发送......&quot;)&#59;
}
}
}
    
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP