Chat, sockets, cliente/servidor... n funciona

2 respostas
D
//servidor

import <a href="http://java.io">java.io</a>.<em>;

import <a href="http://java.net">java.net</a>.</em>;

public class Servidor{

public ServerSocket server;

public Socket sock;

public DataInputStream in;

public DataOutputStream out;

public Servidor(){

try{

this.server = new ServerSocket(8080);

this.sock = this.server.accept();

this.in = new DataInputStream(sock.getInputStream());

this.out = new DataOutputStream(sock.getOutputStream());

}

catch(IOException e){

System.out.println(e.getMessage());

}

}

public static void main(String args[]){

try{

Servidor serv = new Servidor();

int valor = serv.in.readInt();

String texto = serv.in.readUTF();

System.out.println(valor);

System.out.println(texto);

serv.out.writeInt(6000);

serv.out.writeUTF(Olá - Socket Servidor.);

serv.in.close();

serv.out.close();

serv.sock.close();

serv.server.close();

}

catch(IOException e){

System.out.println(e.getMessage());

}

}

}

//cliente

import <a href="http://java.io">java.io</a>.<em>;

import <a href="http://java.net">java.net</a>.</em>;

public class Cliente{

public Socket client;

public DataInputStream in;

public DataOutputStream out;

public Cliente(){

try{

this.client = new Socket(127.0.0.1,8080);

this.in = new DataInputStream(client.getInputStream());

this.out = new DataOutputStream(client.getOutputStream());

}

catch(IOException e){

System.out.println(e.getMessage());

}

}

public static void main(String args[]){

try{

Cliente cli = new Cliente();

cli.out.writeInt(3000);

cli.out.writeUTF(Olá - Socket Cliente.);

int valor = cli.in.readInt();

String texto = cli.in.readUTF();

System.out.println(valor);

System.out.println(texto);

cli.in.close();

cli.out.close();

cli.client.close();

}

catch(IOException e){

System.out.println(e.getMessage());

}

}

}

2 Respostas

augusto_ab

… post o erro…

D

simplesmente nao aparece nada quando executo, ao compilar esta tudo bem…

Criado 8 de janeiro de 2007
Ultima resposta 8 de jan. de 2007
Respostas 2
Participantes 2