public void send(int x,int y)throws IOException //客户端发送消息
{
try{
ServerSocket server=new ServerSocket(9999);
System.out.println("start:"+server);
Socket s=server.accept();
System.out.println("connecting :"+s);
.
.
客户端不是这样连服务器端的,
try{
Socket client = new Socket(服务器名或服务器ip,服务程序的监听端口);
.
.
这样就可以了
{
try{
ServerSocket server=new ServerSocket(9999);
System.out.println("start:"+server);
Socket s=server.accept();
System.out.println("connecting :"+s);
.
.
客户端不是这样连服务器端的,
try{
Socket client = new Socket(服务器名或服务器ip,服务程序的监听端口);
.
.
这样就可以了
解决方案 »
- quartz的问题啊。。。。。。。。。。。哎
- 请各位帮忙:access里面的长二进制数据(类型:ole对象,数据是文字和图片的混合体),用java还原
- 200分在线等待:怎么能实现FTP自动下载文本文件?
- 如何用JFormattedTextField实现只接受double数的操作(50分)
- 怎么在Fram窗体中怎样划线
- 小弟是初學者,一問題請先知者教我一下
- java中private 关键字的问题?
- 100分求救:如何将下面这个小程序转换为一个Applet,等待......
- java的虚函数是怎样定义的,现想将类A中的变量x定义为只能给子类B与自己类中可访问,而又不能给其他类中用实例A a = new A()的a访问,可
- 用Java如何在有菜单和按钮的情况下显示JPG图形文件(要求有垂直和水平的滚动条),而菜单和按钮仍可见????
- jbuilder 9 中设计包(.jar)
- swing问题,怎么样得到一个文件夹的路径?
怎么都叫长啊
这才是一个服务器端的程序啊
还有客户端的呢
写个这样的程序不长也不行啊
看了别人的都写了好几百K呢
我这还不是很多吧 几k
下面的是客户端的程序 别闲长啊 昨天晚上突然停电没有贴上去
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
import java.io.*;
import javax.swing.*;
import java.util.*;
import java.net.*;/*<applet code=wzqClient height=600 width=600>
</applet>
*/public class wzqClient extends JApplet implements ActionListener
{
JButton b1;
JButton b2;
JButton b3;
Panel p1;
qipan q=new qipan();
public wzqClient()
{
}
public void init()
{
getContentPane().setLayout(new BorderLayout());
b1=new JButton("Start");
b2=new JButton("Exit");
b3=new JButton("Last");
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
p1=new Panel();
p1.add(b1);
p1.add(b2);
p1.add(b3);
getContentPane().add("North",p1);
getContentPane().add("Center",q);
}
public void paint(Graphics g)
{
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b1)
{
}
if(e.getSource()==b2)
{
}
if(e.getSource()==b3)
{
}
}
}class qipan extends JApplet implements MouseListener //canvas
{
int maxHeight;
int maxWidth;
int x1=0;
int y1=0; // 自己下的棋子的坐标
String x2=new String();
String y2=new String(); //对方下的棋子的坐标
boolean [][]=new boolean [15][15];
int x[]=new int[15];
int y[]=new int[15];
int position_x=0;
int position_y=0;
boolean win=false;
BufferedReader in;
PrintWriter out;
// Socket client=null;
public qipan()
{
addMouseListener(this);
for(int i=0;i<15;i++)
for(int j=0;j<15;j++)
{
[i][j]=false;
}
/* try{
client=new Socket("219.246.84.224",9999);
System.out.println(client);
}catch(Exception e){System.out.println(client);}*/
}
public void init()
{
System.out.println("dddddddddddddddddddddddddddddd");
}
public void paint(Graphics g)
{
maxHeight=getHeight();
maxWidth=getWidth();
System.out.println(""+maxHeight);
System.out.println(""+maxWidth);
// setBackground(Color.gray);
x[0]=50;
y[0]=50;
for(int i=1;i<15;i++)
{
x[i]=x[i-1]+35;
y[i]=y[i-1]+35;
}
for(int i=0;i<15;i++)
{
g.drawLine(x[0],y[i],x[14],y[i]);
g.drawLine(x[i],y[0],x[i],y[14]);
}
}
public void update(Graphics g)
{
g.setColor(Color.black); //客户端和服务器端设置不同的颜色
draw(position_x,position_y,g);
try{
send(x1,y1);
receive();
g.setColor(Color.white);
draw(x[Integer.parseInt(x2)],y[Integer.parseInt(y2)],g);
}catch(Exception e){System.out.println(e);}
}
public void draw(int x,int y,Graphics g)
{
g.fillArc(position_x-8,position_y-8,16,16,0,360);
isWin();
}
public void send(int x,int y)throws IOException //客户端发送消息
{
Socket client=null;
try
{
client=new Socket("219.246.84.229",9999);
System.out.println("start:"+client);
System.out.println("connecting :"+client);
out=new PrintWriter(client.getOutputStream());
String str=null;
out.println(Integer.toString(x));
out.println(Integer.toString(y));
System.out.println(""+x+y);
client.close();
out.close();
}
catch(Exception e){System.out.println("2"+e);}
finally
{
}
}
public void receive()throws IOException
{
Socket client=null;
try
{
client=new Socket("219.246.84.224",9999);
System.out.println("start:"+client);
System.out.println("connecting :"+client);
in=new BufferedReader(
new InputStreamReader(client.getInputStream()));
x2 = in.readLine();
y2 = in.readLine();
System.out.println(x2+y2);
in.close();
client.close();
}
catch(Exception e){System.out.println("1"+e);}
finally
{
}
}
public void isWin()
{
int num=0; //竖
for(int i=0;i<15;i++)
{
for(int j=0;j<15;j++)
{
if([i][j]==true)
num=num+1;
}
if(num>=5)
{
System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"); //赢了之后的操作
}
num=0;
}
int num1=0; //横
for(int i=0;i<15;i++)
{
for(int j=0;j<15;j++)
{
if([j][i]==true)
num1++;
}
if(num1>=5)
{
System.out.println("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^"); //赢了之后的操作
}
num1=0;
}
int num2=0; //左上右下 下
int m;
for(int j=0;j<11;j++)
{
m=j;
for(int i=0;i<15-j;i++,j++)
{
if([i][j]==true)
System.out.println("*******************************");
num2++;
}
if(num2>=5)
{
//System.out.println("*******************************"); //赢了之后的操作
}
num2=0;
j=m;
}
}
public void mouseClicked(MouseEvent e)
{
for(int i=0;i<15;i++)
{
if(x[i]-10<e.getX()&&e.getX()<x[i]+10)
{
position_x=x[i];
x1=i;
break;
}
}
for(int i=0;i<15;i++)
{
if(y[i]-10<e.getY()&&e.getY()<y[i]+10)
{
position_y=y[i];
y1=i;
break;
}
}
[x1][y1]=true;
repaint();
}
public void mousePressed(MouseEvent e)
{
}
public void mouseReleased(MouseEvent e)
{
}
public void mouseEntered(MouseEvent e)
{
}
public void mouseExited(MouseEvent e)
{
}
}
我实在找不出来毛病了中间实现的时候 判断输赢没有作完
还有就是 zzzhc 说的
我也感觉每次接受 发送时候新建一个socket 不怎么好
给改一下吧没有复杂的动西 实现的特别简单 复制下来应改不会花费太多的时间的谢谢~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
import java.io.*;/**
* @version 1.0
*/public class TestServer extends Thread{
public TestServer() {
} public void run(){
try {
ServerSocket serverSocket = new ServerSocket(5555);
while (true){
Socket client = serverSocket.accept();
if (client != null){
OutputStream out = client.getOutputStream();
InputStream in = client.getInputStream(); byte[] buffer = new byte[10000];
if (in.read(buffer) > 0) {
System.out.println("read:" + new String(buffer).trim());
}
System.out.println("got it");
String report = "got you";
out.write(report.getBytes());
// Thread.sleep(1000); out.flush();
client.close();
System.out.println("report ok");
client.close();
Thread.sleep(50);
}
}
}
catch (Exception ex) {
ex.printStackTrace();
}
} public static void main(String[] args) {
TestServer testServer = new TestServer();
testServer.start();
}}
package testclass;import java.net.*;
import java.io.*;
/**
* @version 1.0
*/public class TestClient extends Thread {
public TestClient() {
} public void run(){
try {
Socket client = new Socket("localhost", 5555);
InputStream in = client.getInputStream();
OutputStream out = client.getOutputStream();
out.write( ("hello").getBytes());
out.flush(); byte[] inputBytes = new byte[20000];
int readPos = 0;
int readNum = 0;
while((readNum=in.read(inputBytes,readPos,20000-readPos))>0){
readPos+=readNum;
}
System.out.println(new String(inputBytes).trim());
}
catch (Exception ex) {
ex.printStackTrace();
}
} public static void main(String[] args) {
TestClient testClient = new TestClient();
testClient.start();
}}
给你个简单的tcp通信的例子吧,希望对你有帮助