我只想要一个程序网络游戏,并且需要进行通信 客户启动...... 服务器是...... 客户给我卡片...... (服务器不响应此代码,如果我必须创建总是新的套接字,我将获得相同的地址.... localhost仅用于测试目的) 套接字只运行一次,我无法理解为什么?????????
我在服务器上正在收听,但它只有一个字符串,并且在下次尝试时不会得到任何东西......
服务器正在聆听和阅读
package client;
import java.net.*;
import java.io.*;
public class Client {
Client() {
try {
Socket socket = new Socket("localhost",2222);
int i=0;
while(i<100){
i++;
OutputStream os = socket.getOutputStream();
PrintWriter pw = new PrintWriter(os,true);
pw.println(i);
}
} catch(IOException e){
System.out.println("Client Constructor Exception");
}
}
public static void main(String[] args) throws Exception{
Client client = new Client();
}
}
服务器代码是.....请帮助我
package server;
import java.io.*;
import java.net.*;
public class Server {
Server() {
try{
serverSocket = new ServerSocket(2222);
} catch(IOException e) {
System.out.println("IOException in Server Constructor");
}
}
public void operate() {
try{
// Control control = new Control();
while (true) {
socket = serverSocket.accept();
InputStream is = socket.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
System.out.println(br.readLine());
//control.recieve(socket, br.readLine());
}
} catch(IOException e) {
System.out.println("IOException in Server operate function");
}
}
public Socket getSocket() {
return socket;
}
public static void main(String[] args) throws IOException {
Server server = new Server();
server.operate();
}
private ServerSocket serverSocket;
private Socket socket;
}
输出是:
运行: 开始
为什么它没有运行100次.....
答案 0 :(得分:5)
每次在循环中创建一个新的套接字,你想得到
socket = new Socket("localhost",2222);
在无限循环之外
答案 1 :(得分:3)
在服务器代码中,您必须在循环外移动server.accept()。 试试这个吧。它会工作。与此同时,我尝试从服务器即时回复。
服务器代码。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
private ServerSocket serverSocket;
private Socket socket;
Server() {
try {
serverSocket = new ServerSocket(2222);
} catch (IOException e) {
System.out.println("IOException in Server Constructor");
}
}
public void operate() {
try {
// Control control = new Control();
socket = serverSocket.accept();
while (true) {
System.out.println("Server starts listening on "
+ serverSocket.getLocalPort());
System.out.println("Server accepts connection.");
OutputStream os = socket.getOutputStream();
PrintWriter pw = new PrintWriter(os, true);
pw.println("Hi Client");
// Thread.sleep(1000);
InputStream is = socket.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
System.out.println(br.readLine());
System.out.println("reply sent to client");
// control.recieve(socket, br.readLine());
}
} catch (IOException /*| InterruptedException*/ e) {
System.out.println("IOException in Server operate function");
}
}
public Socket getSocket() {
return socket;
}
public static void main(String[] args) throws IOException {
Server server = new Server();
server.operate();
}
}
客户代码:
package com.java.examples;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
public class Client {
Client() {
try {
Socket socket;
socket = new Socket("localhost",2222);
// InputStream in = socket.getInputStream();
while (true) {
// socket = new Socket("localhost", 2222);
System.out.println("Socket - " + socket.getLocalPort());
OutputStream os = socket.getOutputStream();
InputStream is = socket.getInputStream();
PrintWriter pw = new PrintWriter(os, true);
pw.println("Hi Server");
// Thread.sleep(2000);
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
System.out.println("Waiting for server reply..");
System.out.println(br.readLine());
System.out.println("Got reply from server..");
// pw.flush();
}
} catch (IOException/* | /*InterruptedException*/ e) {
e.printStackTrace();
System.out.println("Client Constructor Exception");
}
}
public static void main(String[] args) throws Exception {
Client client = new Client();
}
}
答案 2 :(得分:0)
Socket socket = new Socket("localhost",2222);
OutputStream os=null;
PrintWriter pw=null;
while(true){
os = socket.getOutputStream();
pw = new PrintWriter(os,true);
pw.println("Start");
}
答案 3 :(得分:0)
Socket socket = new Socket("localhost",2222);
OutputStream os=null;
PrintWriter pw=null;
while(true){
os = socket.getOutputStream();
pw = new PrintWriter(os,true);
pw.println("Start");
pw.flush(); // to empty the writer buffer
}