anyone good at Java?

I am writing a simple client server chat program.

I need help writing my chatting() command. How do I use the streams? My code is below.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
 /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package chatserver;
import java.io.IOException;
import java.net.*;
import java.util.*;
import java.lang.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.io.*;
/**
 *
 * @author ibraheem
 */
public class Server implements Runnable {
    
    private ServerSocket server;
    private Socket s;
    private OutputStream outstream;
    private InputStream instream;
    private Scanner in;
    private PrintWriter out;


    /**
     * constructor
     */
    public Server()
    {
        //listen for client connections
        try
        { 
            server = new ServerSocket(8888);
            this.setConnection();
            instream = s.getInputStream();
            outstream = s.getOutputStream();
            
        }
        catch(IOException ex)
        {System.out.println("server initialization failed");}  
    }
    
    public void run()
    {
        try{
            try
            {   //set streams
                in = new Scanner(instream);
                out = new PrintWriter(outstream);
                
                //start service
                runServer();
            }
            finally
            {
            s.close();
            }
        }
        catch(IOException e)
        {
            e.printStackTrace();
        }
        
    }
    
    
    //purpose: close streams and sockets
    private void close() throws IOException
    {
        System.out.println("closing Connection");
        s.close();
        outstream.close();
        instream.close();
    }

    
        //configure server and run it
    public void runServer() throws IOException
    {
        while(true)
        {
            try
            {
                chatting();
            }
            catch(Exception ex)
            {
              System.out.println("Chat has been disconnected");
            }
            finally
            {
            close();
            }
        }
    }    
    
    //chat back and forth
    private void chatting()
    {
        
        
    }
     
        
    //waits for connection to chat with
    private void setConnection()
    {
        System.out.println("Connecting...");
      
        //server client connection
        try
        {
        s = server.accept();
        }
        catch(IOException e)
        {
            System.out.println("failed to find connection");
        }    
        
        System.out.println("Connected to " + s.getInetAddress().getHostName());
    }
           
    }

     
    
}
closed account (N36fSL3A)
Are you having trouble with the design or actually sending the data?
sending the data

This is what my client looks like

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package chatclient;

import java.io.*;
import java.util.*;
import java.net.*;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author ibraheem
 */
public class Client implements Runnable{
    
    private ServerSocket server;
    private Socket s;
    private OutputStream outstream;
    private InputStream instream;
    private Scanner in;
    private Scanner inChat;
    private PrintWriter out;
    private String message = "";
    private String serverIP;
    
    public Client(String host)
    {
        inChat = new Scanner(System.in);
        serverIP = host;
        
    }
    
    //connect to server
    public void run()
    {
        try
        {
            connectToServer();
            
            setStreams();
          
            chat();
        }
        catch(Exception e)
        {
            System.out.println("Terminated connection");
        }
        finally
        {
            try
            {
             close();
            }
            catch(IOException e)
            { 
                System.out.println("connectioning to server....");
                e.printStackTrace();
            }
       
        }
   
    }
        

    //connect to server
    private void connectToServer(){
     System.out.println("connectioning to server....");
        try 
        {
            s = new Socket(InetAddress.getByName(serverIP), 8888);
        } 
        catch (IOException ex)
        {
            Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
        }
        
         System.out.println("connected to server" + s.getInetAddress().getHostName());
        
        
    }   
    
    
    //set streams to send and recieve messages
    private void setStreams() throws IOException
    {
        outstream = s.getOutputStream();
        
        instream = s.getInputStream();
        
         System.out.println("streams configured");
        
    }
    
    private void chat()
    {
    
        do{
            try{
                message = inChat.nextLine();
                System.out.println(message);
                
                out.print(message);
                out.flush();
            }catch(Exception e)
            {
                 System.out.println("Error in Chat Method");
            }
        
        }while(!message.equals("LOG OUT"));
        
    }
    
    //close streams
    private void close() throws IOException
    {
        out.close();
        in.close();
        s.close();
    }
}
Last edited on
I think my problem is in line 105 when I try to send the data.
Topic archived. No new replies allowed.