我正在研究一个java客户端 - 服务器程序,但我很难创建一个tcp套接字,它将在发出请求后分别收到一个接一个的消息并分别响应每个消息。因为(-1)又称接收套接字字节期间收到的终止分隔符在套接字关闭后出现。
- 我想要实现的目标是
(暗示插座连接)
客户机GT;>"请求"
服务器>>"响应"
客户机GT;>"请求"
服务器>>"响应"
......等等
我如何在JAVA中这样做? 每次我必须发送和接收消息时,是否必须关闭并打开套接字连接?
很抱歉没有整齐地解释! 提前谢谢!!!
答案 0 :(得分:-1)
MyServer是服务器代码
import java.io.*;
import java.net.*;
public class MyServer {
public static void main(String[] args) throws Exception{
ServerSocket ss=new ServerSocket(6666);
Socket s = ss.accept();//establishes connection
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//BufferedWriter dout = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
DataInputStream din = new DataInputStream(s.getInputStream());
DataOutputStream dout = new DataOutputStream(s.getOutputStream());
String str1 = "";
String str2 = "";
while(!str1.equals("stop")){
str1 = din.readUTF();;
System.out.println("Client: " + str1);
str2 = br.readLine();
dout.writeUTF(str2);
dout.flush();
}
din.close();
s.close();
ss.close();
}
}
MyClient是客户端代码
import java.io.*;
import java.net.*;
public class MyClient {
public static void main(String[] args) throws Exception {
Socket s = new Socket("localhost",6666);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//BufferedReader din = new BufferedReader(new InputStreamReader(s.getInputStream()));
DataInputStream din = new DataInputStream(s.getInputStream());
DataOutputStream dout = new DataOutputStream(s.getOutputStream());
String str1 = "";
String str2 = "";
while(!str1.equals("stop")){
str1 = br.readLine();
dout.writeUTF(str1);
dout.flush();
str2 = din.readUTF();
System.out.println("Server: " + str2);
}
dout.close();
s.close();
}
}
此代码允许客户端和服务器之间的双向通信。 可以通过从任何一方发送停止消息来停止通信。
请记住从客户端发送第一条消息以开始沟通
答案 1 :(得分:-1)
这是服务器多线程的一个示例:
Server.java
import java.net.ServerSocket;
import java.net.Socket;
public class Server implements Runnable{
private ServerSocket socket = null;
private Socket sock = null;
private boolean running = false;
public Server(){
try{
socket = new ServerSocket(4444);
}catch(Exception e){
e.printStackTrace();
}
}
@Override
public void run() {
while(!running){
running = true;
System.out.println("Server : UP");
try{
sock = socket.accept();
}catch(Exception e){
System.out.println("Errore durante l'attesa di nuove connessioni.");
break;
}
ClientThread ct = new ClientThread(sock,this);
Thread t = new Thread(ct);
t.start();
System.out.println("In attesa di connessioni...");
}
}
public void stop(){
if(running){
running = false;
System.out.println("Server : DOWN");
try{
socket.close();
}catch(Exception e){
e.printStackTrace();
return;
}
}
}
public boolean isRunning(){
return running;
}
}
ClientThread.java
package Server;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.NoSuchElementException;
import java.util.Scanner;
public class ClientThread implements Runnable{
private Scanner scan;
private PrintWriter pw;
private Socket sock;
private Server main_thread;
private boolean flag = false;
private boolean running = true;
private SenderThread st = null;
public ClientThread(Socket sock,Server server){
this.sock = sock;
this.main_thread = server;
}
@Override
public void run() {
if(flag)
return;
flag = true;
try{
scan = new Scanner(sock.getInputStream());
pw = new PrintWriter(sock.getOutputStream());
}catch(Exception e){
e.printStackTrace();
}
while(running){
try{
String cmd = scan.nextLine();
if(cmd.equals("Start")){
st = new SenderThread(pw);
Thread t = new Thread(st);
t.start();
}
else if(cmd.equals("Stop")){
st.stop();
}
else{
running = false;
}
}catch(NoSuchElementException e){
running = false;
e.printStackTrace();
}
}
try{
sock.close();
scan.close();
pw.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
Listener.java
package Client;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;
import javax.swing.JOptionPane;
public class Listener implements ActionListener{
private GuiClient frame;
private Socket socket;
private Scanner scan;
private PrintWriter pw;
private Receiver receiver = null;
public Listener(GuiClient gui){
this.frame = gui;
}
@Override
public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand();
if(cmd.equals("Connect")){
if(frame.ip_textfield.getText() == "" || frame.port_textfield.getText() == "" ){
JOptionPane.showMessageDialog(null, "Dati insufficienti !!!");
}
String address = frame.ip_textfield.getText();
int port = Integer.parseInt(frame.port_textfield.getText());
try{
setupConnection(address,port);
frame.status_label.setText("SERVER UP");
}catch(Exception e1){
JOptionPane.showMessageDialog(null, "Impossibile stabilire una connessione con il server.");
e1.printStackTrace();
}
frame.Start.setEnabled(true);
frame.Connect.setEnabled(false);
frame.Disconnect.setEnabled(true);
frame.Stop.setEnabled(false);
}else if(cmd.equals("Start")){
//ricevi stringhe e mostrale a schermo
frame.Start.setEnabled(false);
frame.Stop.setEnabled(true);
frame.Connect.setEnabled(false);
frame.Disconnect.setEnabled(false);
pw.println(cmd);
pw.flush();
try{
receiver = new Receiver(frame.status_label,scan);
}catch(Exception e2){
JOptionPane.showMessageDialog(null, "Impossibile ricevere stringhe...");
e2.printStackTrace();
}
System.out.println("Client : "+cmd);
Thread t = new Thread(receiver);
t.start();
}else if(cmd.equals("Stop")){
frame.Stop.setEnabled(false);
frame.Disconnect.setEnabled(true);
frame.Connect.setEnabled(false);
frame.Start.setEnabled(true);
try{
pw.println(cmd);
pw.flush();
JOptionPane.showMessageDialog(null, "Ricezione delle stringhe interrotta.");
}catch(Exception e1){
e1.printStackTrace();
}
}else if(cmd.equals("Disconnect")){
frame.Connect.setEnabled(true);
frame.Disconnect.setEnabled(false);
frame.Start.setEnabled(false);
frame.Stop.setEnabled(false);
try{
socket.close();
}catch(Exception e1){
e1.printStackTrace();
}
}
}
private void setupConnection(String address, int port){
try{
socket = new Socket(address,port);
OutputStream os = socket.getOutputStream();
scan = new Scanner(socket.getInputStream());
pw = new PrintWriter(os);
}catch(Exception e){
e.printStackTrace();
}
}
}
监听器在gui客户端后面工作; SenderThread和Receiver是其他java类,用于处理字符串(在本例中)。我希望能帮到你。