package Server;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
public class ApplicationServer {
/**
* 服务器端代码
*/
public static void main(String[] args) throws IOException{
// TODO Auto-generated method stub
ServerSocket s = new ServerSocket(3333);
InetAddress addr = InetAddress.getByName("localhost");
System.out.println("The address is:"+addr);
System.out.println("The Server is start:"+s);
Socket socket = s.accept();
try{
System.out.println("Accept the Client:"+socket);
//设置I/O句柄
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true);
String str = in.readLine();
System.out.println("the str is :"+str);
if(!str.equals("xushiyong")){
System.out.println("valid failed!");
out.println("reduse server");
System.exit(0);
}
else {
System.out.println("accept server");
out.println("accept server");
while (true){
String serverItem = in.readLine();
if(serverItem.equals("byebye"))
{
break;
}
else{
provideServer(serverItem);
out.println("finish server");
}
}
}
}
finally{
System.out.println("close the Server socket and the io");
socket.close();
s.close();
}
}
public static void provideServer(String serverItem){
if(serverItem.equals("1")){
System.out.println("provide the file server");
}
else if(serverItem.equals("2")){
System.out.println("provide the sql server");
}
}
}
package Client;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
public class ApplicationClient {
/**
* 客户端程序
*/
public static int portNo = 3333;
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
InetAddress addr = InetAddress.getByName("localhost");
Socket socket = new Socket(addr,portNo);
try{
System.out.println("socket ="+socket);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true);
out.println("xushiyong");
System.out.println("send the xushiyong");
String echo = in.readLine();
System.out.println("the echo is:"+echo);
if(echo.equals("refuse server")){
System.out.println("refuse server");
out.println("byebye");
}
else if(echo.equals("accept server")){
System.out.println("accept server");
while(true){
String serverEcho;
out.println("1");
serverEcho = in.readLine();
System.out.println("serverEcho is"+serverEcho);
if(serverEcho.equals("finish server")){
out.println("byebye");
break;
}
}
}
}
finally{
System.out.println("close the Client socket and the io");
socket.close();
}
}
}
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
public class ApplicationServer {
/**
* 服务器端代码
*/
public static void main(String[] args) throws IOException{
// TODO Auto-generated method stub
ServerSocket s = new ServerSocket(3333);
InetAddress addr = InetAddress.getByName("localhost");
System.out.println("The address is:"+addr);
System.out.println("The Server is start:"+s);
Socket socket = s.accept();
try{
System.out.println("Accept the Client:"+socket);
//设置I/O句柄
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true);
String str = in.readLine();
System.out.println("the str is :"+str);
if(!str.equals("xushiyong")){
System.out.println("valid failed!");
out.println("reduse server");
System.exit(0);
}
else {
System.out.println("accept server");
out.println("accept server");
while (true){
String serverItem = in.readLine();
if(serverItem.equals("byebye"))
{
break;
}
else{
provideServer(serverItem);
out.println("finish server");
}
}
}
}
finally{
System.out.println("close the Server socket and the io");
socket.close();
s.close();
}
}
public static void provideServer(String serverItem){
if(serverItem.equals("1")){
System.out.println("provide the file server");
}
else if(serverItem.equals("2")){
System.out.println("provide the sql server");
}
}
}
package Client;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
public class ApplicationClient {
/**
* 客户端程序
*/
public static int portNo = 3333;
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
InetAddress addr = InetAddress.getByName("localhost");
Socket socket = new Socket(addr,portNo);
try{
System.out.println("socket ="+socket);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true);
out.println("xushiyong");
System.out.println("send the xushiyong");
String echo = in.readLine();
System.out.println("the echo is:"+echo);
if(echo.equals("refuse server")){
System.out.println("refuse server");
out.println("byebye");
}
else if(echo.equals("accept server")){
System.out.println("accept server");
while(true){
String serverEcho;
out.println("1");
serverEcho = in.readLine();
System.out.println("serverEcho is"+serverEcho);
if(serverEcho.equals("finish server")){
out.println("byebye");
break;
}
}
}
}
finally{
System.out.println("close the Client socket and the io");
socket.close();
}
}
}