服务器给客户端传6个数,客户端根据6个数画圆,我的代码如下:
客户端:import java.io.*;
import java.net.*;
import java.awt.*;class drawcil extends Frame{ //画图类
public int [] vir=new int[7];
drawcil(int [] as){
super();
vir=as;
setSize(300,300);
setVisible(true);
setBackground(Color.RED);
}
public void paint(Graphics g){
g.fillArc(vir[1], vir[2], vir[3], vir[4],vir[5],vir[6]);
}
}
public class Client { //客户端
public int [] virb=new int[7];
public Client() {
}
/** Creates a new instance of TcpClient */
public static void main(String[] args)throws IOException{
Client cl=new Client();
cl.go(args);
}
public void go(String[] args)throws IOException {
Socket soc=null;
BufferedReader in=null;
PrintStream out=null;
String strin=null;
int temp,i=0;
try
{if(args.length<1)
System.out.println("CommandParam should > 1");
else{
soc=new Socket(args[0],8000);
System.out.println("Connecting to the Server.....");
in=new BufferedReader(new InputStreamReader(soc.getInputStream()));
strin=in.readLine();
System.out.println("Server said:"+strin);
strin=in.readLine();
while(!strin.equalsIgnoreCase("ok")){
if(!strin.equalsIgnoreCase("00")){ //
System.out.println("server said:"+strin); //
strin=in.readLine(); //客户端接受服务器数据,同时赋给virb
temp=Integer.parseInt(strin);
virb[i]=temp;
i++;
}
else{
drawcil frame=new drawcil(virb);//生成类drawcil的实列,画圆,但没实现,
//只弄了很多框架出来
}
}
}}
catch(Exception e)
{
System.out.println("error:"+e);
}
finally
{
in.close();
soc.close();
System.exit(0);
}
}}
服务器:应该没有问题
import java.net.*;
import java.io.*;
public class TcpServer {
/** Creates a new instance of TcpServer */
public TcpServer() {
}
public static void main(String[] args) throws IOException {
ServerSocket svrsoc=null;
Socket soc=null;
BufferedReader in=null;
PrintWriter out=null;
InetAddress clientIP=null;
BufferedReader sysin=null;
String strout=null;
try{
svrsoc=new ServerSocket(8000);
System.out.println("Server start....");
soc=svrsoc.accept();
in=new BufferedReader(new InputStreamReader(soc.getInputStream()));
out=new PrintWriter(soc.getOutputStream(),true);
clientIP=soc.getInetAddress();
System.out.println("Client's IP address is:"+clientIP + " and Port is:"+soc.getLocalPort());
out.println("Welcome....");
System.out.println("press enter and input 6 numbers end with 00 (if you want to exit input ok):");
sysin=new BufferedReader(new InputStreamReader(System.in));
strout=sysin.readLine();
while (!strout.equalsIgnoreCase("ok"))
{
out.println(strout);
strout=sysin.readLine();
}
out.println(strout);
}
catch(Exception e){
e.printStackTrace();
}
finally{
in.close();
out.close();
svrsoc.close();
soc.close();
System.exit(0);
}
}
}
谢谢各位了!!!!!
客户端:import java.io.*;
import java.net.*;
import java.awt.*;class drawcil extends Frame{ //画图类
public int [] vir=new int[7];
drawcil(int [] as){
super();
vir=as;
setSize(300,300);
setVisible(true);
setBackground(Color.RED);
}
public void paint(Graphics g){
g.fillArc(vir[1], vir[2], vir[3], vir[4],vir[5],vir[6]);
}
}
public class Client { //客户端
public int [] virb=new int[7];
public Client() {
}
/** Creates a new instance of TcpClient */
public static void main(String[] args)throws IOException{
Client cl=new Client();
cl.go(args);
}
public void go(String[] args)throws IOException {
Socket soc=null;
BufferedReader in=null;
PrintStream out=null;
String strin=null;
int temp,i=0;
try
{if(args.length<1)
System.out.println("CommandParam should > 1");
else{
soc=new Socket(args[0],8000);
System.out.println("Connecting to the Server.....");
in=new BufferedReader(new InputStreamReader(soc.getInputStream()));
strin=in.readLine();
System.out.println("Server said:"+strin);
strin=in.readLine();
while(!strin.equalsIgnoreCase("ok")){
if(!strin.equalsIgnoreCase("00")){ //
System.out.println("server said:"+strin); //
strin=in.readLine(); //客户端接受服务器数据,同时赋给virb
temp=Integer.parseInt(strin);
virb[i]=temp;
i++;
}
else{
drawcil frame=new drawcil(virb);//生成类drawcil的实列,画圆,但没实现,
//只弄了很多框架出来
}
}
}}
catch(Exception e)
{
System.out.println("error:"+e);
}
finally
{
in.close();
soc.close();
System.exit(0);
}
}}
服务器:应该没有问题
import java.net.*;
import java.io.*;
public class TcpServer {
/** Creates a new instance of TcpServer */
public TcpServer() {
}
public static void main(String[] args) throws IOException {
ServerSocket svrsoc=null;
Socket soc=null;
BufferedReader in=null;
PrintWriter out=null;
InetAddress clientIP=null;
BufferedReader sysin=null;
String strout=null;
try{
svrsoc=new ServerSocket(8000);
System.out.println("Server start....");
soc=svrsoc.accept();
in=new BufferedReader(new InputStreamReader(soc.getInputStream()));
out=new PrintWriter(soc.getOutputStream(),true);
clientIP=soc.getInetAddress();
System.out.println("Client's IP address is:"+clientIP + " and Port is:"+soc.getLocalPort());
out.println("Welcome....");
System.out.println("press enter and input 6 numbers end with 00 (if you want to exit input ok):");
sysin=new BufferedReader(new InputStreamReader(System.in));
strout=sysin.readLine();
while (!strout.equalsIgnoreCase("ok"))
{
out.println(strout);
strout=sysin.readLine();
}
out.println(strout);
}
catch(Exception e){
e.printStackTrace();
}
finally{
in.close();
out.close();
svrsoc.close();
soc.close();
System.exit(0);
}
}
}
谢谢各位了!!!!!
drawcil 是1个FRAME阿。。你有1句
drawcil frame=new drawcil(virb);//生成类drawcil的实列,画圆,但没实现,
就是这句。。每次都新来1个frame当然只要接受倒server数据然后是园就会新建窗口出来
一般得这么农窝用的是JFrame JPanel
public MyCircle
{
private Point _center = null;
private int _length = 0;
public MyCircle(Point c, int l)
{
_center = c; _lenth = l;
}
public Point getCenter()
{
return _center;
}
public int getLength()
{
return _length;
}
{
public class MYPanel extends JPanel
{
private Vector _circles = new Vector(); public void addNewCircle(MyCircle circle)
{
_circles.add(circle);
}
protected void paintComponent(Graphics g)
{
for(Iterator i = _circles.iterator();i.hasNext();)
{
MyCircle circle = (MyCircle)i.next();
g.drawOval(circle.getCenter().getX(),circle.getCenter().getY(),circle.getLegnth());
}
}
}自己看看吧。。也没有测试过。。但以前做过。。就是这么来得咯再给你个建议。。你可以直接传送MyCircle 的object阿。。这样方便很多
然后自己做个协议包
public class MyProtocol
{
private String _head;
private MyShape _shape;
.....
}
然后你的shape都extends MyShape
这样你的程序就可以传输很多图形了
在awt一样可以用。。自己看一下java的api document把还有这儿高手不少的。就是不大上来罢了。。赫赫。。我问问题就挂着。。顶顶。。看看问别人是要有耐心的阿。。