Pessoal,
Estou fazendo uma conex�o entre cliente/servidor para enviar mensagem ou arquivo em
ambos os sentidos. Algu�m sabe me dizer onde estou errando?
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
public class ServerS extends Thread{
String Smsg;
String msg;
String inmsg;
private ServerSocket serverSocket;
public ServerS(){
super("Servidor");
try{
serverSocket = new ServerSocket(6789);
System.out.println("Criei um Servidor...");
}
catch(IOException e){
System.err.println("Exception: couldn't create socket");
System.exit(1);
}
}
public static void main (String args[ ] ) {
ServerS srv;
srv = new ServerS();
srv.start();
}
public void run(){
Socket clientSocket = null;
while (true){
if (serverSocket == null)
return;
try{
clientSocket = serverSocket.accept();
}
catch(IOException e){
System.err.println("Exception: couldn't connect to client
socket");
System.exit(1);
}
// Recebe a mensagem via Socket do cliente
PrintWriter os;
try{
InputStreamReader inr = new
InputStreamReader(clientSocket.getInputStream());
BufferedReader in = new BufferedReader(inr);
os = new PrintWriter(clientSocket.getOutputStream(),true);
while ( (inmsg = in.readLine( )) != null )
{
//Imprime a mensagem localmente
System.out.println(inmsg);
}
} catch (IOException e) { System.err.println(e.toString( ));}
// }
System.out.println("Entre com uma mensagem......");
try {
//Recebendo a mensagem via teclado a ser enviada ao Cliente
DataInputStream msg;
msg = new DataInputStream(System.in);
Smsg = msg.readLine();
}catch (IOException i) {
System.out.println("Erro !");
}
//Enviando a mensagem ao cliente...
try {
PrintStream ps = new
PrintStream(clientSocket.getOutputStream());
ps.println(Smsg);
}
catch (IOException e)
{
System.out.println("Error: " + e);
}
}
}
}
// Cliente
import java.io.*;
import java.net.*;
class Cliente {
String msg;
private ServerSocket serverSocket;
String line;
static String Omsg;
public static void main (String args[ ] ) {
Cliente cli;
cli = new Cliente();
DataInputStream lemsg;
DataInputStream asg;
//DataInputStream msg;
lemsg = new DataInputStream(System.in);
// Abre o Socket
try{
Socket s = new Socket("Localhost",6789);
// Escolhe a op��o de digitar a mensagem ou ler um arquivo
System.out.println("Digite mm para mensagem ou aa para arquivo....");
try {
DataInputStream msg;
asg = new DataInputStream(System.in);
cli.Omsg = asg.readLine();
}catch (IOException i) {
System.out.println("Erro !");
}
//Caso a op��o seja digitar a mensagem......
String str1 = "mm";
if (Omsg.equals (str1))
{
System.out.println("Entre com uma mensagem....");
try {
DataInputStream msg;
msg = new DataInputStream(System.in);
cli.msg = lemsg.readLine();
}catch (IOException i) {
System.out.println("Erro !");
}
// Enviando a mensagem ao Servidor...
try {
//Socket s = new Socket("Localhost",6789);
PrintStream out;
out = new PrintStream(s.getOutputStream( ));
out.println(cli.msg);
}
catch (IOException e)
{
System.out.println("Error: " + e);
}
}
// Caso a op��o seja ler um arquivo......
String str2 = "aa";
if (Omsg.equals (str2))
{
try {
// Socket s = new Socket("Localhost",6789);
PrintStream out;
out = new PrintStream(s.getOutputStream( ));
//out.println(cli.line);
//System.out.println(cli.line);
FileReader file = new
FileReader("porra.txt");
BufferedReader buff = new
BufferedReader(file);
boolean eof = false;
String teste="";
while (!eof)
{
cli.line = buff.readLine();
out.println(cli.line);
if (cli.line == null)
eof = true;
//else
//System.out.println(cli.line); Para imprimir o mesmo
arquivo localmente
}
buff.close();
//s.close( );
}catch (IOException i) {
System.err.println (i.toString( ));
}
}
//Recebe a mensagem do Servidor
System.out.println("Resp:");
DataInputStream dis = new DataInputStream(s.getInputStream());
String msg = dis.readLine();
System.out.println(msg);
}
catch(IOException e){
System.err.println("Exception: couldn't create socket");
System.exit(1);
}
}
}
------------------------------ LISTA SOUJAVA ----------------------------
http://www.soujava.org.br - Sociedade de Usu�rios Java da Sucesu-SP
d�vidas mais comuns: http://www.soujava.org.br/faq.htm
regras da lista: http://www.soujava.org.br/regras.htm
historico: http://www.mail-archive.com/java-list%40soujava.org.br
para sair da lista: envie email para [EMAIL PROTECTED]
-------------------------------------------------------------------------