Exemplo de Server usando Socket (Simples)

1 resposta
F

Falae moçada,

Estou tendo um problema ao rodar um código usando sockets, não sei se é porque estou usando um único computador por isso não funciona... mas meu professor fez usando o laptop dele e funcionou...

O Código é o seguinte:

Server.java
import java.io.*;
import java.net.*;
import java.lang.*;
import java.util.*;

public class Server
{

	static public void main( String args[] )
	{
		try
		{
			new Server();
		}
		catch( Exception e )
		{
			System.out.println( e.getMessage() );
			e.printStackTrace();
		}
	}

	Server() throws Exception
	{
		int session = 1;
		ServerSocket ss = new ServerSocket(8765);
		while ( true )
		{
			System.out.println( "Aguardando porta 8765" );
			Socket s = ss.accept();
			System.out.println( "Conexao efetuada" );
			AtendeClient cs = new AtendeClient(s,session++);
			Thread t = new Thread(cs);
			t.start();
		}
	}
}

class AtendeClient implements Runnable {

	Socket s;
	InputStream is;
	OutputStream os;
	int session;

	AtendeClient( Socket s, int session ) throws Exception
	{
		this.s = s;
		System.out.println( "Iniciando sessao" );
		this.is = s.getInputStream();
		this.os = s.getOutputStream();
		this.session = session;
	}

	public void run()
	{
		byte buffer[] = new byte[31];
		try
		{
			while ( true ) {
				is.read( buffer );
				System.out.println( "Dados recebidos client ("+session+")=>"+( new String(buffer) ) );
				os.write( buffer );
				os.flush();
			}
		}
		catch( Exception e )
		{
			System.out.println( e.getMessage() );
		}
	}
}

E me aparece um erro:

init:
deps-jar:
Compiling 1 source file to C:\Documents and Settings\Rodolfo\SimpleServer\build\classes
compile-single:
run-single:
java.lang.NoClassDefFoundError: simpleserver/SimpleServer
Exception in thread "main"
Java Result: 1
BUILD SUCCESSFUL (total time: 2 seconds)

VOcês poderiam me dizer o que pode estar acontecendo?

Obrigadoooooo!

1 Resposta

E

hum…

como você está tentando executar?
qual a versão do seu java?
as variáveis de ambiente estão configuradas?

confere esse post sobre problemas de execução
de um programa java atraves do prompt de comando :wink:

abraços

Criado 17 de maio de 2006
Ultima resposta 17 de mai. de 2006
Respostas 1
Participantes 2