Komple Kod
Her sınıfı ayrı dosyalarda ve sınıf adı ile kaydetmeyi unutmayın. Yani chatserver-sınıfını chatserver.java", baglanti sınıfı "baglanti.java" ve chatapplet-sınıfını "chatapplet.java" olarak kaydedin.
import java.net.*;
import java.io.*;
import java.util.*;
public class chatserver implements Runnable
{
public static final int PORT = 8888;
protected ServerSocket dinle;
protected Vector baglantilar;
Thread baglan;
public chatserver()
{
try
{
dinle = new ServerSocket(PORT);
} catch (IOException e)
{
System.err.println("Socket oluşturulurken hata :"+e);
System.exit(1);
}
baglantilar = new Vector();
baglan = new Thread(this);
baglan.start();
}
public **** run()
{
try
{
while(true)
{
Socket istemci=dinle.accept();
baglanti b = new baglanti(this, istemci);
baglantilar.addElement(b);
}
} catch (IOException e)
{
System.err.println("Bağlantı talebi beklerken hata :"+e);
System.exit(1);
}
}
public static **** main(String[] args)
{
new chatserver();
}
public **** mesajgonder(String msg)
{
int i;
baglanti sen;
for (i=0; i<baglantilar.size(); i++)
{
sen = (baglanti) baglantilar.elementAt(i);
sen.out.println(msg);
}
}
}
import java.net.*;
import java.io.*;
class baglanti extends Thread
{
protected Socket istemci;
protected DataInputStream in;
protected PrintStream out;
protected chatserver server;
public baglanti(chatserver server, Socket istemci)
{
this.server=server;
this.istemci=istemci;
try
{
in = new DataInputStream(istemci.getInputStream());
out = new PrintStream(istemci.getOutputStream());
} catch (IOException e)
{
try { istemci.close(); } catch (IOException e2) {} ;
System.err.println("Stream oluşturulurken hata : " + e);
return;
}
this.start();
}
public **** run()
{
String satir;
try
{
while(true)
{
satir=in.readLine();
if(satir!=null)
server.mesajgonder(satir);
}
} catch (IOException e)
{ System.out.println("Hata:" + e);
}
}
}
import java.net.*;
import java.io.*;
import java.awt.*;
import java.applet.*;
public class chatapplet extends Applet implements Runnable
{
public static final int PORT = 8888;
Socket socket;
DataInputStream in;
PrintStream out;
TextField inputfield;
TextArea outputarea;
Thread thread;
public **** init()
{
inputfield = new TextField();
outputarea = new TextArea();
outputarea.setFont( new Font("Dialog", Font.PLAIN, 12));
outputarea.setEditable(false);
this.setLayout(new BorderLayout());
this.add("South", inputfield);
this.add("Center", outputarea);
this.setBackground(Color.lightGray);
this.setForeground(Color.black);
inputfield.setBackground(Color.white);
outputarea.setBackground(Color.white);
}
public **** start()
{
try
{
socket = new Socket(this.getCodeBase().getHost(), PORT);
in = new DataInputStream(socket.getInputStream());
out = new PrintStream(socket.getOutputStream());
} catch (IOException e)
{
this.showStatus(e.toString());
say("Server ile bağlantı kurulamadı!");
System.exit(1);
}
say("Server ile bağlantı kuruldu...");
if (thread == null)
{
thread = new Thread(this);
thread.setPriority(Thread.MIN_PRIORITY);
thread.start();
}
}
public **** stop()
{
try
{
socket.close();
} catch (IOException e)
{
this.showStatus(e.toString());
}
if ((thread !=null) && thread.isAlive())
{
thread.stop();
thread = null;
}
}
public **** run()
{
String satir;
try
{
while(true)
{
satir = in.readLine();
if(satir!=null)
outputarea.appendText(satir+'
' );
}
} catch (IOException e) { say("Server ile bağlantı kesildi."); }
}
public boolean action(Event e, Object what)
{
if (e.target==inputfield)
{
String inp=(String) e.arg;
out.println(inp);
inputfield.setText("");
return true;
}
return false;
}
public **** say(String msg)
{
outputarea.appendText("*** "+msg+" ***
");
}
}
Her sınıfı ayrı dosyalarda ve sınıf adı ile kaydetmeyi unutmayın. Yani chatserver-sınıfını chatserver.java", baglanti sınıfı "baglanti.java" ve chatapplet-sınıfını "chatapplet.java" olarak kaydedin.
import java.net.*;
import java.io.*;
import java.util.*;
public class chatserver implements Runnable
{
public static final int PORT = 8888;
protected ServerSocket dinle;
protected Vector baglantilar;
Thread baglan;
public chatserver()
{
try
{
dinle = new ServerSocket(PORT);
} catch (IOException e)
{
System.err.println("Socket oluşturulurken hata :"+e);
System.exit(1);
}
baglantilar = new Vector();
baglan = new Thread(this);
baglan.start();
}
public **** run()
{
try
{
while(true)
{
Socket istemci=dinle.accept();
baglanti b = new baglanti(this, istemci);
baglantilar.addElement(b);
}
} catch (IOException e)
{
System.err.println("Bağlantı talebi beklerken hata :"+e);
System.exit(1);
}
}
public static **** main(String[] args)
{
new chatserver();
}
public **** mesajgonder(String msg)
{
int i;
baglanti sen;
for (i=0; i<baglantilar.size(); i++)
{
sen = (baglanti) baglantilar.elementAt(i);
sen.out.println(msg);
}
}
}
import java.net.*;
import java.io.*;
class baglanti extends Thread
{
protected Socket istemci;
protected DataInputStream in;
protected PrintStream out;
protected chatserver server;
public baglanti(chatserver server, Socket istemci)
{
this.server=server;
this.istemci=istemci;
try
{
in = new DataInputStream(istemci.getInputStream());
out = new PrintStream(istemci.getOutputStream());
} catch (IOException e)
{
try { istemci.close(); } catch (IOException e2) {} ;
System.err.println("Stream oluşturulurken hata : " + e);
return;
}
this.start();
}
public **** run()
{
String satir;
try
{
while(true)
{
satir=in.readLine();
if(satir!=null)
server.mesajgonder(satir);
}
} catch (IOException e)
{ System.out.println("Hata:" + e);
}
}
}
import java.net.*;
import java.io.*;
import java.awt.*;
import java.applet.*;
public class chatapplet extends Applet implements Runnable
{
public static final int PORT = 8888;
Socket socket;
DataInputStream in;
PrintStream out;
TextField inputfield;
TextArea outputarea;
Thread thread;
public **** init()
{
inputfield = new TextField();
outputarea = new TextArea();
outputarea.setFont( new Font("Dialog", Font.PLAIN, 12));
outputarea.setEditable(false);
this.setLayout(new BorderLayout());
this.add("South", inputfield);
this.add("Center", outputarea);
this.setBackground(Color.lightGray);
this.setForeground(Color.black);
inputfield.setBackground(Color.white);
outputarea.setBackground(Color.white);
}
public **** start()
{
try
{
socket = new Socket(this.getCodeBase().getHost(), PORT);
in = new DataInputStream(socket.getInputStream());
out = new PrintStream(socket.getOutputStream());
} catch (IOException e)
{
this.showStatus(e.toString());
say("Server ile bağlantı kurulamadı!");
System.exit(1);
}
say("Server ile bağlantı kuruldu...");
if (thread == null)
{
thread = new Thread(this);
thread.setPriority(Thread.MIN_PRIORITY);
thread.start();
}
}
public **** stop()
{
try
{
socket.close();
} catch (IOException e)
{
this.showStatus(e.toString());
}
if ((thread !=null) && thread.isAlive())
{
thread.stop();
thread = null;
}
}
public **** run()
{
String satir;
try
{
while(true)
{
satir = in.readLine();
if(satir!=null)
outputarea.appendText(satir+'
' );
}
} catch (IOException e) { say("Server ile bağlantı kesildi."); }
}
public boolean action(Event e, Object what)
{
if (e.target==inputfield)
{
String inp=(String) e.arg;
out.println(inp);
inputfield.setText("");
return true;
}
return false;
}
public **** say(String msg)
{
outputarea.appendText("*** "+msg+" ***
");
}
}