Reputation: 251
I'm trying to pick up Java, this is only my second day, so I apologize if this is somewhat elementary. I am creating a chat client that connects to a server that I have previously created (it was written in C). The client program is set up so that it has an outTextArea
, inTextArea
, and a sendButton
(pretty basic stuff, but still - getting the basics down). I want the chat client to open the socket when the program is opened, which is currently opened in main
(connection information hidden):
Socket sock = new Socket("theServer", 0000);
I know the program connects correctly, and remains open- the server current prints when an IP connects to the server, and then seg faults when a connection is closed.
Here is the problem: I am trying to send a string that was typed in outTextArea
over that socket when I click the sendButton
:
public void send(String outbuffer) throws Exception {
sendButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
sendMessage sender = new sendMessage();
sender.send(outTextArea.getText());
}
});
The sendMessage class:
System.out.println(outbuffer);
PrintStream printer = new PrintStream(sock.getOutputStream());
printer.println(outbuffer);
This is where I realized I can't send over sock
, that was created in the main
class.
I'm curious as to how I can access the socket that was created, if there is an alternative way to send a message without needing to create a new connection each time. Any advice is greatly appreciated, thank you.
Upvotes: 2
Views: 231
Reputation: 1544
There are a bunch of different ways to accomplish this. The easiest and quickest way to do it with the code you already have in place, is to pass the socket through as a parameter to the sendMessage class.
Socket sock; //declare sock UP TOP so it's global
public static void main(String[] args) {
Socket sock = new Socket("server", 0000);
... other stuff
}
public void send(String outbuffer) throws Exception {
sendButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
sendMessage sender = new sendMessage(sock);
sender.send(outTextArea.getText());
}
}
});
}
Then inside of the sendMessage class, you'll want its constructor to grab the socket that's passed in.
public class sendMessage {
private Socket sock;
public sendMessage(Socket sock) {
this.sock = sock;
}
public void send(String message) {
......
}
}
Upvotes: 1
Reputation: 13133
There are two basic ways -- either the socket can be an instance variable in a class that contains both the socket and the method using it, or the caller can pass the socket to the method. In the latter case, the method would have a second parameter in addition to the String it has now. There are other possibilities, but one of these should fit your case best.
If I might just deliver an opinion as well: socket programming seems a bit advanced for someone who is still figuring out things about variable scope and parameter passing. You might try for simpler things to get the basics down better before attempting to troubleshoot networking problems.
Upvotes: 1