Bom pessoal, to postando para vocês um exemplo bem simples de como fazer um MSN utilizando Socket's em JAVA!
MODO DE UTILIZAR:
só basta executar as duas classes no mesmo computador.
MODO DE UTILIZAR:
só basta executar as duas classes no mesmo computador.
Janela1.java
Código PHP:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package Chat;
import java.io.*;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
/**
*
* @author ArthurPaixao
*/
public class Janela1 extends Frame //implements ActionListener
{
Button send;
TextField tf;
TextArea ta;
String str;
public Janela1() {
setLayout(null);
setTitle("ChatBox - ArthurPaixão");
setBackground(Color.LIGHT_GRAY);
setForeground(Color.black);
setSize(500, 450);
setVisible(true);
tf = new TextField(20);
ta = new TextArea(10, 10);
send = new Button("ENVIAR");
add(ta);
ta.setBounds(30, 50, 430, 300);
add(tf);
tf.setBounds(30, 380, 350, 40);
add(send);
send.setBounds(390, 380, 70, 40);
addWindowListener(new wh());
send.addActionListener(new send());
while (true) {
try {
ServerSocket s = new ServerSocket(3000);
Socket c = s.accept();
BufferedReader b = new BufferedReader(new InputStreamReader(c.getInputStream()));
str = b.readLine();
ta.append("\n");
ta.append(str);
} catch (Exception e) {
}
}
}
class wh extends WindowAdapter {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
}
class send implements ActionListener {
public void actionPerformed(ActionEvent ae) {
String str1 = ae.getActionCommand();
if (str1.equals("ENVIAR")) {
try {
String s1 = tf.getText();
InetAddress address = InetAddress.getLocalHost();
String mm = address.getHostName();
// Se quiser utilizar um host, só colocar o ip:
// String i="172.16.77.214;
String i = "localhost";
Socket s = new Socket(i, 4000);
PrintWriter out = null;
ta.append("\n");
ta.append(mm + " : " + s1);
out = new PrintWriter(s.getOutputStream());
out.println(mm + " : " + s1);
out.flush();
} catch (Exception e) {
}
}
}
}
public static void main(String args[]) throws IOException {
Janela1 ff = new Janela1();
}
}
Código PHP:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package Chat;
/**
*
* @author ArthurPaixao
*/
import java.io.*;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
public class Janela2 extends Frame //implements ActionListener
{
Button send;
TextField tf;
TextArea ta;
//System.out.println(address);
//String s=new Integer(address).toString();
//ta.setText(address);
String str;
public Janela2() {
setLayout(null);
setTitle("ChatBox - ArthurPaixão");
setBackground(Color.BLACK);
setForeground(Color.BLUE);
setSize(500, 450);
setVisible(true);
tf = new TextField(20);
ta = new TextArea(10, 10);
send = new Button("ENVIAR");
add(ta);
ta.setBounds(30, 50, 430, 300);
add(tf);
tf.setBounds(30, 380, 350, 40);
add(send);
send.setBounds(390, 380, 70, 40);
addWindowListener(new wh());
send.addActionListener(new send());
while (true) {
try {
ServerSocket s = new ServerSocket(4000);
Socket c = s.accept();
BufferedReader b = new BufferedReader(new InputStreamReader(c.getInputStream()));
str = b.readLine();
ta.append("\n");
ta.append(str);
} catch (Exception e) {
}
}
}
class wh extends WindowAdapter {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
}
class send implements ActionListener {
public void actionPerformed(ActionEvent ae) {
String str1 = ae.getActionCommand();
if (str1.equals("ENVIAR")) {
try {
String s1 = tf.getText();
InetAddress address = InetAddress.getLocalHost();
String mm = address.getHostName();
String i = "localhost";
Socket s = new Socket(i, 3000);
PrintWriter out = null;
ta.append("\n");
ta.append(mm + " : " + s1);
out = new PrintWriter(s.getOutputStream());
out.println(mm + " : " + s1);
out.flush();
} catch (Exception e) {
}
}
}
}
public static void main(String args[]) throws IOException {
Janela2 ff = new Janela2();
}
}
Comment