利用Java编写一个程序利用Socket,实现网络的点对点文件传输并且能实现聊天,发个给我学习下,好吗? 20
展开全部
这个时比较简单的。
package QQ;
import java.net.*;
import java.io.*;
import java.sql.*;
import java.util.*;
public class QQServer {
public static void main(String args[]) {
try {
HashMap<String , Socket> hm = new
HashMap<String , Socket>() ;
// 服务器到8000端口监听(1)
ServerSocket ss = new ServerSocket
(8000);
while (true) {
System.out.println("服务器正在
8000端口监听.....");
Socket s = ss.accept();
MyService t = new MyService();
t.setSocket(s) ;
t.setHashMap(hm) ;
t.start();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
class MyService extends Thread {
private Socket s ;
private HashMap<String , Socket> hm ;
public void setHashMap(HashMap<String , Socket> hm){
this.hm = hm ;
}
public void setSocket(Socket s){
this.s = s ;
}
public void run() {
try {
// 接收客户端发送来的用户名和密码(2)
InputStream is = s.getInputStream();
InputStreamReader isr = new InputStreamReader
(is);
BufferedReader br = new BufferedReader(isr);
String uandp = br.readLine();
// 拆分用户名和密码(4)
String u = "";
String p = "";
try{
u = uandp.split("%")[0];
p = uandp.split("%")[1];
}catch(Exception e){}
// 到数据库中验证(5)
Class.forName
("com.microsoft.jdbc.sqlserver.SQLServerDriver");
Connection cn = DriverManager
.getConnection(
"jdbc:microsoft:sqlserver://127.0.0.1:1433;databasename=qq2",
"sa", "123");
PreparedStatement ps = cn
.prepareStatement("select
username from username where username=? and password=?");
ps.setString(1, u);
ps.setString(2, p);
ResultSet rs = ps.executeQuery();
// 发送确认信息到客户端(7)
OutputStream os = s.getOutputStream();
OutputStreamWriter osw = new
OutputStreamWriter(os);
PrintWriter pw = new PrintWriter(osw, true);
if (rs.next()) {
pw.println("ok");
//将自己的名字发送给HashMap中的其他人
(13)
for(Socket ts : hm.values()){
OutputStream tos =
ts.getOutputStream() ;
OutputStreamWriter tosw = new
OutputStreamWriter(tos) ;
PrintWriter tpw = new
PrintWriter(tosw , true) ;
tpw.println("user%"+u) ;
}
//将其他人的名字发送给自己(13)
for(String tu : hm.keySet()){
pw.println("user%"+tu) ;
}
//将用户名和对应的Socket存入HashMap
(13)
hm.put(u, s) ;
// 接收客户端发送来的信息(11)
while (true) {
String message = br.readLine
();
if(message.equals("exit")){
for(Socket ts :
hm.values()){
OutputStream
tos = ts.getOutputStream() ;
OutputStreamWriter tosw = new OutputStreamWriter(tos) ;
PrintWriter
tpw = new PrintWriter(tosw , true) ;
tpw.println
("exit%"+u);
}
hm.remove(u);
}
String to = message.split
("%")[0];
String mess = message.split
("%")[1];
Socket ts = hm.get(to);
OutputStream tos =
ts.getOutputStream();
OutputStreamWriter tosw = new
OutputStreamWriter(tos);
PrintWriter tpw = new
PrintWriter(tosw, true);
tpw.println("mess%"+mess
+"\n");
System.out.println(message);
}
} else {
pw.println("err");
}
} catch (Exception e) {}
}
}
package QQ;
import java.net.*;
import java.io.*;
import java.sql.*;
import java.util.*;
public class QQServer {
public static void main(String args[]) {
try {
HashMap<String , Socket> hm = new
HashMap<String , Socket>() ;
// 服务器到8000端口监听(1)
ServerSocket ss = new ServerSocket
(8000);
while (true) {
System.out.println("服务器正在
8000端口监听.....");
Socket s = ss.accept();
MyService t = new MyService();
t.setSocket(s) ;
t.setHashMap(hm) ;
t.start();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
class MyService extends Thread {
private Socket s ;
private HashMap<String , Socket> hm ;
public void setHashMap(HashMap<String , Socket> hm){
this.hm = hm ;
}
public void setSocket(Socket s){
this.s = s ;
}
public void run() {
try {
// 接收客户端发送来的用户名和密码(2)
InputStream is = s.getInputStream();
InputStreamReader isr = new InputStreamReader
(is);
BufferedReader br = new BufferedReader(isr);
String uandp = br.readLine();
// 拆分用户名和密码(4)
String u = "";
String p = "";
try{
u = uandp.split("%")[0];
p = uandp.split("%")[1];
}catch(Exception e){}
// 到数据库中验证(5)
Class.forName
("com.microsoft.jdbc.sqlserver.SQLServerDriver");
Connection cn = DriverManager
.getConnection(
"jdbc:microsoft:sqlserver://127.0.0.1:1433;databasename=qq2",
"sa", "123");
PreparedStatement ps = cn
.prepareStatement("select
username from username where username=? and password=?");
ps.setString(1, u);
ps.setString(2, p);
ResultSet rs = ps.executeQuery();
// 发送确认信息到客户端(7)
OutputStream os = s.getOutputStream();
OutputStreamWriter osw = new
OutputStreamWriter(os);
PrintWriter pw = new PrintWriter(osw, true);
if (rs.next()) {
pw.println("ok");
//将自己的名字发送给HashMap中的其他人
(13)
for(Socket ts : hm.values()){
OutputStream tos =
ts.getOutputStream() ;
OutputStreamWriter tosw = new
OutputStreamWriter(tos) ;
PrintWriter tpw = new
PrintWriter(tosw , true) ;
tpw.println("user%"+u) ;
}
//将其他人的名字发送给自己(13)
for(String tu : hm.keySet()){
pw.println("user%"+tu) ;
}
//将用户名和对应的Socket存入HashMap
(13)
hm.put(u, s) ;
// 接收客户端发送来的信息(11)
while (true) {
String message = br.readLine
();
if(message.equals("exit")){
for(Socket ts :
hm.values()){
OutputStream
tos = ts.getOutputStream() ;
OutputStreamWriter tosw = new OutputStreamWriter(tos) ;
PrintWriter
tpw = new PrintWriter(tosw , true) ;
tpw.println
("exit%"+u);
}
hm.remove(u);
}
String to = message.split
("%")[0];
String mess = message.split
("%")[1];
Socket ts = hm.get(to);
OutputStream tos =
ts.getOutputStream();
OutputStreamWriter tosw = new
OutputStreamWriter(tos);
PrintWriter tpw = new
PrintWriter(tosw, true);
tpw.println("mess%"+mess
+"\n");
System.out.println(message);
}
} else {
pw.println("err");
}
} catch (Exception e) {}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询