Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Exception in thread "Thread-0" java.lang.ClassCastException:
- java.io.ObjectStreamClass cannot be cast to message.Mesaj
- at server.ServerThread.readAndWrite(ServerThread.java:43)
- at server.ServerThread.run(ServerThread.java:61)
- java.io.StreamCorruptedException: invalid type code: 00
- at java.io.ObjectInputStream.readObject0(Unknown Source)
- at java.io.ObjectInputStream.readObject(Unknown Source)
- at server.ServerThread.readAndWrite(ServerThread.java:43)
- at server.ServerThread.run(ServerThread.java:61)
- package client;
- import java.io.IOException;
- import java.io.ObjectInputStream;
- import java.io.ObjectOutputStream;
- import java.net.Socket;
- import java.util.Scanner;
- import message.Mesaj;
- public class Client {
- public static int port=4321;
- public static Socket socket;
- public static ObjectOutputStream oo;
- public static ObjectInputStream oi;
- public static Scanner sc;
- public Client() throws IOException{
- socket = new Socket ("localhost",4321);
- oi = new ObjectInputStream(socket.getInputStream());
- oo = new ObjectOutputStream(socket.getOutputStream());
- }
- public static void listen() throws ClassNotFoundException, IOException{
- while(true){
- Mesaj m = (Mesaj) oi.readObject();
- if(m!=null){
- System.out.println("mesajul este: " + m.getMesaj());
- }
- }
- }
- public static void write() throws IOException{
- sc= new Scanner(System.in);
- while(true){
- String trimite= sc.nextLine();
- Mesaj m = new Mesaj();
- m.setMesaj(trimite);
- oo.writeObject(m);
- oo.flush();
- }
- }
- public static Thread t = new Thread(){
- public void run(){
- try {
- listen();
- } catch (ClassNotFoundException | IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- };
- public static Thread t2 = new Thread(){
- public void run(){
- try {
- write();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- };
- public static void main(String[] args) throws IOException{
- new Client();
- t.start();
- t2.start();
- }
- package server;
- import java.io.IOException;
- import java.net.ServerSocket;
- import java.net.Socket;
- import java.util.ArrayList;
- public class Server {
- public int port;
- public static Socket socket;
- public static ServerSocket serverSocket;
- public Server() throws IOException{
- this.port=4321;
- serverSocket = new ServerSocket(port);
- }
- public static void main (String [] args){
- try {
- new Server();
- } catch (IOException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- }
- System.out.println("Server functionabil..asteptam conexiune client");
- while(true){
- try {
- socket= serverSocket.accept();
- ServerThread st= new ServerThread(socket);
- st.start();
- System.out.println("Conexiune realizata -client conectat");
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (ClassNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- }
- package server;
- import java.io.IOException;
- import java.io.ObjectInputStream;
- import java.io.ObjectOutputStream;
- import java.net.Socket;
- import java.util.ArrayList;
- import message.Mesaj;
- public class ServerThread extends Thread {
- boolean running;
- public static ObjectOutputStream oo;
- public static ObjectInputStream oi;
- public static Mesaj m;
- public static Socket socket;
- public ServerThread(Socket socket) throws ClassNotFoundException{
- try {
- running=true;
- this.socket=socket;
- oo = new ObjectOutputStream(socket.getOutputStream());
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- public static void readAndWrite() throws ClassNotFoundException, IOException{
- oi = new ObjectInputStream(socket.getInputStream());
- while(true){
- m= (Mesaj) oi.readObject();
- if(m!=null){
- oo.writeObject(m);
- oo.flush();
- System.out.println(m.getMesaj());
- }
- }
- }
- public void run(){
- System.out.println("Server Thread contectat");
- try {
- readAndWrite();
- } catch (IOException | ClassNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement