Answer the question
In order to leave comments, you need to log in
I can't connect to the server from another computer, what should I do?
There is this client:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;
public class Client {
static JTextArea messages;
static JTextField NewMessage;
static BufferedReader reader;
static PrintWriter writer;
static Socket sock;
public static void main(String[] args) {
Client client = new Client();
JFrame f = new JFrame();
f.setTitle("ChatMessenger");
JPanel p = new JPanel();
messages = new JTextArea(15, 30);
messages.setLineWrap(true);
messages.setWrapStyleWord(true);
messages.setEditable(false);
JScrollPane qscroller = new JScrollPane(messages);
qscroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
qscroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
NewMessage = new JTextField(20);
JButton sendButton = new JButton("Отправить");
sendButton.setBackground(Color.lightGray);
sendButton.addActionListener(new SendButtonListener());
p.add(qscroller);
p.add(NewMessage);
p.add(sendButton);
setUpNetworking();
Thread readerThread = new Thread(new IncomingReader());
readerThread.start();
f.getContentPane().add(BorderLayout.CENTER, p);
f.setSize(400, 500);
f.setVisible(true);
}
private static void setUpNetworking() {
try {
sock = new Socket("192.168.100.12", 50693);
InputStreamReader streamReader = new InputStreamReader(sock.getInputStream());
reader = new BufferedReader(streamReader);
writer = new PrintWriter(sock.getOutputStream());
System.out.println("Подключение установлено!");
} catch (Exception e) {
System.out.println(e);
}
}
static class SendButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
try {
writer.println(NewMessage.getText());
writer.flush();
} catch (Exception ex) {
System.out.println(ex);
}
NewMessage.setText("");
NewMessage.requestFocus();
}
}
static class IncomingReader implements Runnable {
public void run() {
String message;
try {
while((message = reader.readLine() ) != null){
messages.append(message + "\n");
}
} catch (Exception ex) {
System.out.println(ex);
}
}
}
}
package chatmessenger;
import java.io.*;
import java.net.*;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Server implements Runnable{
static Socket client;
static BufferedReader reader;
static ServerSocket server;
static ArrayList <PrintWriter> OutputStreams;
public static void main(String[]args) throws IOException {
OutputStreams = new ArrayList() ;
server = new ServerSocket(50693);
System.out.println("Запущен сервер!");
while(true){
client = server.accept();
InputStreamReader isr = new InputStreamReader(client.getInputStream());
reader = new BufferedReader(isr);
PrintWriter sender = new PrintWriter(client.getOutputStream());
OutputStreams.add(sender);
Thread t = new Thread(new Server());
t.start();
System.out.println("Подключился клиент!");
send("Chat : Kto-to zashel! (Russkiy poka chto ne podderzhivaetsa))) )");
}
}
static String message;;
public void run() {
try{
while((message = reader.readLine()) != null){
System.out.println(message);
send(message);
}
}catch(Exception e){
System.out.println(e);
}
}
static public void send(String message){
for(PrintWriter sender : OutputStreams ){
sender.println(message);
sender.flush();
}
}
}
Answer the question
In order to leave comments, you need to log in
I would first check the firewall settings and open port 50693 on the server, turn off the antivirus (if it is Windows) ...
It would also be good to see where SocketServer is attached.
In Linux, you can see the command netstat -an | grep 50693
, in Windows, there is probably something similar too ...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question