Uygulama 5050 portunda çalışmaktadır.Client Server dan sayı ister ve Server 6 tane sayı gönderir.
Server Sınıfı
import java.io.IOException;
import java.io.PrintWriter;
import java.net.*;
public class ChatServer {
String[] adviceList = {"1","2","3","4","5","6","7","8","9","10","11","12"};
public void go() {
try {
ServerSocket c=new ServerSocket(5050);
while(true)
{
Socket s=c.accept();
PrintWriter writer=new PrintWriter(s.getOutputStream());
String mesaj=getMesaj();
writer.println(mesaj);
writer.close();
System.out.println("Server’ın seçtiği sayı: " + mesaj);
}
}
catch (IOException e) {
e.printStackTrace();
}
}
private String getMesaj() {
int random = (int) (Math.random() * adviceList.length);
return adviceList[random];
}
public static void main(String[] args) {
ChatServer server=new ChatServer();
server.go();
}
}
Client Sınıfı
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.Socket;
public class ChatClient {
public void go() {
try {
int i=0;
while(i<6)
{
Socket s=new Socket("localhost",5050);
InputStreamReader streamReader=new InputStreamReader(s.getInputStream());
BufferedReader reader=new BufferedReader(streamReader);
String mesaj=reader.readLine();
System.out.println("Sayı Gönder:"+mesaj);
++i;
reader.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
ChatClient chat=new ChatClient();
chat.go();
}
}
mehmet
13/06/2010 — 19:54
elimde aşağıdaki gibi kod var ve kullanıcalr istediği kadar kullanıcı ekleyebilcek yardımcı olursanız
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
public class SpaceShip extends JFrame implements KeyListener, MouseListener, Runnable
{
int k=0;
public static void main(String[] args)
{ new SpaceShip().setVisible(true); }
Thread runner;
int speed = 100; //sleeps for 100 milliseconds, decrease if you want faster and increse if you want it slower
JLabel lblShip = new JLabel(new ImageIcon(“c://spaceship.jpg”));
JLabel lblShip2 = new JLabel(new ImageIcon(“c://spaceship1.jpg”));
//1. gemi değişkenleri:
static int x = 450, y = 0, width = 50, height = 50, incAmountX = 0, incAmountY = 0;
boolean shoot = false;
int shootX = 0, shootY = 0;
//2. gemi değişkenleri:
static int x2 = 0, y2 = 200, width2 = 50, height2 = 50, incAmountX2 = 0, incAmountY2 = 0;
boolean shoot2 = false;
int shootX2 = 0, shootY2 = 0;
public SpaceShip()
{
super(“SpaceShip Wars”);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500, 500);
setLayout(null);
lblShip.setBounds(x, y, 35, 30);
add(lblShip);
lblShip2.setBounds(x2, y2, 35, 30);
add(lblShip2);
addKeyListener(this);
addMouseListener(this);
runner = new Thread(this);
runner.start();
}
public void keyPressed(KeyEvent e)
{
shoot = shoot2 = false;
switch(e.getKeyCode())
{
//1. gemi kontrolleri ok tuşları
case KeyEvent.VK_UP: incAmountY = -1; incAmountX = 0; break;
case KeyEvent.VK_DOWN: incAmountY = 1; incAmountX = 0; break;
case KeyEvent.VK_LEFT: incAmountX = -1; incAmountY = 0; break;
case KeyEvent.VK_RIGHT: incAmountX = 1; incAmountY = 0; break;
//2. gemi kontrolleri w-a-s-d tuşları
case KeyEvent.VK_W: incAmountY2 = -1; incAmountX2 = 0; break;
case KeyEvent.VK_S: incAmountY2 = 1; incAmountX2 = 0; break;
case KeyEvent.VK_A: incAmountX2 = -1; incAmountY2 = 0; break;
case KeyEvent.VK_D: incAmountX2 = 1; incAmountY2 = 0; incAmountX3 = 1; incAmountY3 = 0;
break;
//gemileri durdurmak için escape
case KeyEvent.VK_ESCAPE: incAmountX = incAmountY = incAmountX2 = incAmountY2 = 0; break;
}
lblShip.setBounds(x, y, width, height);
lblShip2.setBounds(x2, y2, width2, height2);
}
public void run()
{
while(true)
{
try
{
runner.sleep(speed);
x += incAmountX;
y += incAmountY;
lblShip.setBounds(x, y, width, height);
x2 += incAmountX2;
y2 += incAmountY2;
lblShip2.setBounds(x2, y2, width2, height2);
lblShip3.setBounds(x3, y3, width3, height3);
x3 += incAmountX3;
y3 += incAmountY3;
repaint();
}
catch (InterruptedException e)
{ e.printStackTrace(); }
}
}
public void paint(Graphics g)
{
super.paint(g);
if(shoot)
g.drawLine(shootX, shootY, x + 20, y + 50);
else if(shoot2)
g.drawLine(shootX2, shootY2, x2 + 20, y2 + 50);
}
public void mousePressed(MouseEvent e)
{
//sol tuş basılıysa 1. gemi ateş eder:
if(e.getButton() == 1)
{
shoot = true;
shootX = e.getX();
shootY = e.getY();
System.out.println(shootX + ” ” + shootY + ” : ” + x2 + ” “+ y2);
//2. gemi menzilde mi diye kontrol et, eğer menzildeyse oyunu bitir:
if(shootX >= x2 – 60 && shootX = y2 – 80 && shootY = x – 30 && shootX2 = y – 40 && shootY2 <= y + 40)
{
runner.stop();
JOptionPane.showMessageDialog(null, "2. gemi 1. gemiyi vurdu, oyun bitti");
}
}
repaint();
}
//elini mouse'tan kaldırınca ateşi kessin.
public void mouseReleased(MouseEvent arg0)
{
shoot = shoot2 = false;
repaint();
}
public void keyReleased(KeyEvent arg0)
{ }
public void keyTyped(KeyEvent arg0)
{ }
public void mouseClicked(MouseEvent e)
{ }
public void mouseEntered(MouseEvent arg0)
{ }
public void mouseExited(MouseEvent arg0)
{ }
}