Advertisement
Guest User

Untitled

a guest
Feb 8th, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.44 KB | None | 0 0
  1. Exception in thread "Thread-0" java.lang.ClassCastException:
  2. java.io.ObjectStreamClass cannot be cast to message.Mesaj
  3. at server.ServerThread.readAndWrite(ServerThread.java:43)
  4. at server.ServerThread.run(ServerThread.java:61)
  5. java.io.StreamCorruptedException: invalid type code: 00
  6. at java.io.ObjectInputStream.readObject0(Unknown Source)
  7. at java.io.ObjectInputStream.readObject(Unknown Source)
  8. at server.ServerThread.readAndWrite(ServerThread.java:43)
  9. at server.ServerThread.run(ServerThread.java:61)
  10.  
  11. package client;
  12.  
  13. import java.io.IOException;
  14. import java.io.ObjectInputStream;
  15. import java.io.ObjectOutputStream;
  16. import java.net.Socket;
  17. import java.util.Scanner;
  18. import message.Mesaj;
  19.  
  20. public class Client {
  21.  
  22. public static int port=4321;
  23. public static Socket socket;
  24. public static ObjectOutputStream oo;
  25. public static ObjectInputStream oi;
  26. public static Scanner sc;
  27.  
  28. public Client() throws IOException{
  29. socket = new Socket ("localhost",4321);
  30. oi = new ObjectInputStream(socket.getInputStream());
  31. oo = new ObjectOutputStream(socket.getOutputStream());
  32.  
  33. }
  34.  
  35.  
  36. public static void listen() throws ClassNotFoundException, IOException{
  37. while(true){
  38. Mesaj m = (Mesaj) oi.readObject();
  39. if(m!=null){
  40. System.out.println("mesajul este: " + m.getMesaj());
  41. }
  42. }
  43. }
  44.  
  45. public static void write() throws IOException{
  46. sc= new Scanner(System.in);
  47. while(true){
  48. String trimite= sc.nextLine();
  49. Mesaj m = new Mesaj();
  50. m.setMesaj(trimite);
  51.  
  52. oo.writeObject(m);
  53. oo.flush();
  54.  
  55. }
  56.  
  57. }
  58.  
  59.  
  60. public static Thread t = new Thread(){
  61. public void run(){
  62.  
  63. try {
  64. listen();
  65. } catch (ClassNotFoundException | IOException e) {
  66. // TODO Auto-generated catch block
  67. e.printStackTrace();
  68. }
  69.  
  70.  
  71. }
  72. };
  73.  
  74. public static Thread t2 = new Thread(){
  75. public void run(){
  76. try {
  77. write();
  78. } catch (IOException e) {
  79. // TODO Auto-generated catch block
  80. e.printStackTrace();
  81. }
  82. }
  83. };
  84.  
  85. public static void main(String[] args) throws IOException{
  86. new Client();
  87. t.start();
  88. t2.start();
  89.  
  90. }
  91.  
  92. package server;
  93.  
  94. import java.io.IOException;
  95. import java.net.ServerSocket;
  96. import java.net.Socket;
  97. import java.util.ArrayList;
  98.  
  99. public class Server {
  100. public int port;
  101. public static Socket socket;
  102. public static ServerSocket serverSocket;
  103.  
  104.  
  105. public Server() throws IOException{
  106. this.port=4321;
  107. serverSocket = new ServerSocket(port);
  108.  
  109. }
  110.  
  111. public static void main (String [] args){
  112. try {
  113. new Server();
  114. } catch (IOException e1) {
  115. // TODO Auto-generated catch block
  116. e1.printStackTrace();
  117. }
  118. System.out.println("Server functionabil..asteptam conexiune client");
  119. while(true){
  120. try {
  121. socket= serverSocket.accept();
  122. ServerThread st= new ServerThread(socket);
  123. st.start();
  124. System.out.println("Conexiune realizata -client conectat");
  125.  
  126. } catch (IOException e) {
  127. // TODO Auto-generated catch block
  128. e.printStackTrace();
  129. } catch (ClassNotFoundException e) {
  130. // TODO Auto-generated catch block
  131. e.printStackTrace();
  132. }
  133. }
  134.  
  135. }
  136. }
  137.  
  138. package server;
  139.  
  140. import java.io.IOException;
  141. import java.io.ObjectInputStream;
  142. import java.io.ObjectOutputStream;
  143. import java.net.Socket;
  144. import java.util.ArrayList;
  145.  
  146. import message.Mesaj;
  147.  
  148. public class ServerThread extends Thread {
  149.  
  150. boolean running;
  151. public static ObjectOutputStream oo;
  152. public static ObjectInputStream oi;
  153. public static Mesaj m;
  154. public static Socket socket;
  155.  
  156.  
  157.  
  158.  
  159. public ServerThread(Socket socket) throws ClassNotFoundException{
  160. try {
  161. running=true;
  162. this.socket=socket;
  163. oo = new ObjectOutputStream(socket.getOutputStream());
  164.  
  165.  
  166. } catch (IOException e) {
  167. // TODO Auto-generated catch block
  168. e.printStackTrace();
  169. }
  170.  
  171. }
  172.  
  173.  
  174.  
  175. public static void readAndWrite() throws ClassNotFoundException, IOException{
  176.  
  177. oi = new ObjectInputStream(socket.getInputStream());
  178.  
  179. while(true){
  180. m= (Mesaj) oi.readObject();
  181. if(m!=null){
  182. oo.writeObject(m);
  183. oo.flush();
  184.  
  185. System.out.println(m.getMesaj());
  186.  
  187. }
  188. }
  189. }
  190.  
  191.  
  192.  
  193.  
  194.  
  195. public void run(){
  196. System.out.println("Server Thread contectat");
  197. try {
  198. readAndWrite();
  199. } catch (IOException | ClassNotFoundException e) {
  200. // TODO Auto-generated catch block
  201. e.printStackTrace();
  202. }
  203. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement