高分求java编程,急用!!!
发布网友
发布时间:2022-05-09 14:08
我来回答
共3个回答
热心网友
时间:2024-02-01 12:36
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Date;
public class Server {
public static void main(String[] args) {
ServerSocket ss = null;
Socket s = null;
PrintWriter pw = null;
BufferedReader br = null;
try {
ss = new ServerSocket(8888);
System.out.println("server is running");
while (true) {
s = ss.accept();
br=new BufferedReader(new InputStreamReader(s.getInputStream()));
String str=br.readLine();
if(str.equalsIgnoreCase("hello")){
System.out.println("Receive Hello");
pw = new PrintWriter(s.getOutputStream());
pw.println("How r u doing");
pw.flush();
}else if(str.equalsIgnoreCase("bye")){
pw = new PrintWriter(s.getOutputStream());
pw.println("I don’t wanna die");
pw.flush();
System.exit(0);
}else{
pw = new PrintWriter(s.getOutputStream());
pw.println("please input correct data");
pw.flush();
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (pw != null)
pw.close();
if (s != null)
try {
s.close();
} catch (IOException e) {
}
if (ss != null)
try {
ss.close();
} catch (IOException e) {
}
}
}
}
---------------
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;
public class Client {
public static void main(String[] args) {
Socket s = null;
BufferedReader br = null;
PrintWriter pw = null;
try {
s = new Socket("127.0.0.1", 8888);
System.out.println("client is running");
while (true) {
BufferedReader brin = new BufferedReader(new InputStreamReader(
System.in));
System.out.println("please input your words:");
String str = brin.readLine();
pw = new PrintWriter(s.getOutputStream());
pw.println(str);
pw.flush();
br = new BufferedReader(new InputStreamReader(s
.getInputStream()));
str = br.readLine();
if (str.equals("How r u doing")) {
System.out.println("Received How are you doing");
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null)
try {
br.close();
} catch (IOException e) {
}
if (s != null)
try {
s.close();
} catch (IOException e) {
}
}
}
}
热心网友
时间:2024-02-01 12:36
给你例子不知能否帮得上你,这是一个服务器接收客户端发送的数字,对其进行*2运算把结果返还给客户端。
服务器端代码:
package exp;
import java.io.*;
import java.net.*;
public class Service {
public static void main(String [] args){
ServerSocket server=null;
Socket you=null;
String s=null;
DataInputStream in=null;
DataOutputStream out=null;
try{
server=new ServerSocket(4331);
}catch(IOException e1){ System.out.println(e1);}
try{System.out.println("等待客户呼叫");
you=server.accept();
out=new DataOutputStream(you.getOutputStream());
in=new DataInputStream(you.getInputStream());
while(true)
{
s=in.readUTF();
int m=Integer.parseInt(s);
out.writeUTF("你好:我是服务器");
out.writeUTF("你说的数乘2后是:"+2*m);
System.out.println("服务器收到:"+s);
Thread.sleep(2000);
}
}catch(Exception e){
System.out.println("客户已断开");
}
}
}
客户端代码:
package exp;
import java.io.*;
import java.net.*;
public class Client {
public static void main(String[] args) {
String s=null;
Socket mysocket;
DataInputStream in=null;
DataOutputStream out=null;
try{
mysocket=new Socket("127.0.0.1",4331);
in=new DataInputStream(mysocket.getInputStream());
out=new DataOutputStream(mysocket.getOutputStream());
for(int k=1;k<100;k=k+2){
out.writeUTF(""+k);
s=in.readUTF();
System.out.println("客户收到:"+s);
Thread.sleep(2000);
}
}catch(Exception e){ System.out.println("服务器已断开"+e);
}
}
}
不明白,白度我
热心网友
时间:2024-02-01 12:36
一看就是交作业用的...没劲!