怎样安装和配置java服务器
JavaWeb Tomcat服务器配置过程如下:
Tomcat服务器端口的配置
Tomcat的所有配置都放在conf文件夹之中,里面的serverxml文件是配置的核心文件。如果想修改Tomcat服务器的启动端口,则可以在serverxml配置文件中的Connector节点进行的端口修改
例如:将Tomcat服务器的启动端口由默认的8080改成8081端口
Tomcat服务器启动端口默认配置
1 <Connector port="8080" protocol="HTTP/11"
2 connectionTimeout="20000"
3 redirectPort="8443" />
将Tomcat服务器启动端口修改成8081端口
1 <Connector port="8081" protocol="HTTP/11"
2 connectionTimeout="20000"
3 redirectPort="8443" />
这样就把原来默认Tomcat默认的的8080端口改成了8081端口了,需要注意的是,一旦服务器中的xml文件改变了,则Tomcat服务器就必须重新启动,重新启动之后将重新读取新的配置信息。因为已经在serverxml文件中将Tomcat的启动端口修改成了8081,所以Tomcat服务器启动时就以8081端口启动了,如下图所示:
服务器监听端口 做个无限循环 接到一个连接就创建一个通道线程,并将通道线程存储到一个list集合中
import javaioBufferedReader;import javaioIOException;
import javaioInputStreamReader;
import javaioPrintWriter;
import javanetServerSocket;
import javanetSocket;
import javatextSimpleDateFormat;
import javautilArrayList;
import javautilDate;
import javautilList;
/
4用socket通讯写出多个客户端和一个服务器端的通讯,
要求客户发送数据后能够回显相同的数据(回显功能)(实用TCP方式)。
/
public class Test4Server {
// 主入口
public static void main(String[] args) throws IOException {
scoketServer();
}
// 开启的tcp8888监听端口
public static void scoketServer() throws IOException {
ServerSocket server = new ServerSocket(8888);
while (true) {
// 未连通前线程阻塞,连通后开启一个socket通道线程后继续监听8888端口
Socket socket = serveraccept();
Systemoutprintln(socketgetInetAddress()getHostAddress()
+ "连接进入");
new SocketThread(socket)start();
}
}
}
// 一个服务器端口中监听多个客服端通道线程
class SocketThread extends Thread {
// 所有通道写入流的集合
private static List<PrintWriter> list =new ArrayList<PrintWriter>();
private BufferedReader bufferedReader;
private PrintWriter printWriter;
public SocketThread(Socket socket) throws IOException {
thisbufferedReader = new BufferedReader(new InputStreamReader(socket
getInputStream()));
thisprintWriter = new PrintWriter(socketgetOutputStream());
listadd(printWriter);
}
@Override
public void run() {
String string = null;
while (true) {
try {
// 服务器在通道中读到的信息回显给客服端
string = bufferedReaderreadLine();
Systemoutprintln("客服端信息:" + string);
for(PrintWriter printWriter:list ){
printWriterwrite("服务器回显:" + string + "\r\n");
printWriterflush();
}
} catch (IOException e) {
}
}
}
}
客服端代码 可以用在局域网中用多台来连接测试
import javaioBufferedReader;import javaioIOException;
import javaioInputStreamReader;
import javaioPrintWriter;
import javanetSocket;
import javautilScanner;
public class Test4Client {
public static Object obj = new Object();
// 客服端的主入口
public static void main(String[] args) throws IOException {
socketClient();
}
// 与服务器连通地址本机(127001),局域网中其他机器是(服务器在局域网中的ip地址) 端口都是8888
public static void socketClient() throws IOException {
Socket socket = new Socket("127001", 8888);
if (socketisConnected()) {
// 如果连接成功了就开启写和读的进程
new writer(socket)start();
new read(socket)start();
} else {
Systemoutprintln("服务器未开启");
}
}
}
// 写入到通道的线程
class writer extends Thread {
@SuppressWarnings("unused")
private Socket socket;
private PrintWriter printWriter;
private Scanner scanner = new Scanner(Systemin);
private String str = null;
public writer(Socket socket) throws IOException {
thissocket = socket;
thisprintWriter = new PrintWriter(socketgetOutputStream());
}
@Override
public void run() {
scanneruseDelimiter("\r\n");
while (true) {
Systemoutprint("请输入信息:");
// 产生扫描器的线程阻塞
str = scannernext();
Systemoutprintln("我说:"+str);
printWriterwrite(str + "\r\n");
printWriterflush();
try {
Threadsleep(200);
} catch (InterruptedException e) {
eprintStackTrace();
}
}
}
}
// 从通道中读取的线程
class read extends Thread {
private Socket socket;
private BufferedReader bufferedReader;
private String str = null;
public read(Socket socket) throws IOException {
thissocket = socket;
thisbufferedReader = new BufferedReader(new InputStreamReader(socket
getInputStream()));
}
@Override
public void run() {
while (true) {
try {
str = bufferedReaderreadLine();
Systemoutprintln(str);
} catch (IOException e) {
}
try {
Threadsleep(200);
} catch (InterruptedException e) {
eprintStackTrace();
}
}
}
}
0条评论