您的位置:首页 > 理论基础 > 计算机网络

初学网络编程 今天的网络编程作业

2017-08-08 21:12 351 查看
1. 使用基于TCP的Java Socket编程,完成如下功能:

1) 要求从客户端录入几个字符,发送到服务器端。

2) 由服务器端将接收到的字符进行输出。

3) 服务器端向客户端发出“您的信息已收到”作为响应。

4) 客户端接收服务器端的响应信息。

public class Client {
public static void main(String[] args) {
System.out.println("-----------客户端-----------");
try {
Socket s = new Socket("127.0.0.1", 8888);
OutputStream os = s.getOutputStream();
InputStream is = s.getInputStream();
OutputStreamWriter osw = new OutputStreamWriter(os);
InputStreamReader isr = new InputStreamReader(is);
String ss = "你好,我是客户端";
osw.write(ss);
osw.flush();
char[] c = new char[1024];
int len = isr.read(c);
String se = new String(c, 0, len);
System.out.println("服务器端说:" + se);
osw.close();
isr.close();
os.close();
is.close();
s.close();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}

public class Server {
public static void main(String[] args) {
System.out.println("-------------服务器端-------------");
try {
ServerSocket ss = new ServerSocket(8888);
Socket a = ss.accept();
OutputStream os = a.getOutputStream();
InputStream is = a.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
char[] c = new char[1024];
int len = isr.read(c);
String se = new String(c, 0, len);
System.out.println("客户端说:" + se);
OutputStreamWriter osw = new OutputStreamWriter(os);
String sc = "服务器端收到了";
osw.write(sc);
osw.flush();
isr.close();
osw.close();
is.close();
os.close();
ss.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
1. 使用TCP网络编程完成用户登录功能:客户端输入用户名和密码,向服务器发出登录请求;服务器接收数据并进行判断,如果用户名和密码均是bjsxt,则登录成功,否则登录失败,返回相应响应信息;客户端接收响应信息并输出登录结果。

1) 用户User类已提供构造方法 public User(String username,String password)。

2) 客户端采用ObjectOutputStream发送封装了用户名和密码的User对象。

public class User implements Serializable {
String username;
String password;

public User(String username, String password) {
this.username = username;
this.password = password;
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

}

public class LoginClient {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入用户名:");
String username = sc.nextLine();
System.out.println("请输入密码:");
String password = sc.nextLine();
User u = new User(username, password);
try {
Socket s = new Socket("127.0.0.1", 8888);
OutputStream os = s.getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(os);
oos.writeObject(u);
oos.flush();
InputStream is = s.getInputStream();
DataInputStream dis = new DataInputStream(is);
String utf = dis.readUTF();
System.out.println(utf);
dis.close();
is.close();
oos.close();
os.close();
s.close();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}

public class LoginServer {
static Map<String, String> map = new HashMap<String, String>();
static {
map.put("bjsxt", "bjsxt");
map.put("zzsxt", "zzsxt");
map.put("shsxt", "shsxt");
map.put("whsxt", "whsxt");
}
static int num = 0;

public static void main(String[] args) {
ServerSocket ss = null;
Socket s = null;
ObjectInputStream ois = null;
DataOutputStream dos = null;
try {
ss = new ServerSocket(8888);
while (true) {
s = ss.accept();
InputStream is = s.getInputStream();
ois = new ObjectInputStream(is);
User u = (User) ois.readObject();
String username = u.getUsername();
String p
4000
assword = u.getPassword();
String m = map.get(username);
String str;
if (m != null && m.equals(password)) {
num++;
str = "恭喜你,登陆成功!您是第" + num + "位访客";
} else {
str = "用户名密码错误";
}
OutputStream os = s.getOutputStream();
dos = new DataOutputStream(os);
dos.writeUTF(str);
dos.flush();
}
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
try {
s.close();
dos.close();
ois.close();
ss.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: