A. 利用java编写一个程序利用Socket,实现网络的点对点文件传输并且能实现聊天,发个给我学习下,好吗
这个时比较简单的。
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) {}
}
}
B. 用JAVA写一个SOCKET 接收TCP发来的消息
服务端监听:ServerSocket server=new ServerSocket(port);//port:绑定的端口号
Socket client=server.accept()();//监听端口,一旦取得连接则获得客户端的socket连接对象client
客户端: Socket s=new Socket(ip,port);//要连接的服务器的ip以及端口号
如果正常连接上之后,socket的对象可以获得InputStream和OutputStreame,然后就可以进行通信了
完成通信之后,执行socket对象的close()方法关闭连接,完成一次完整的socket连接
C. 大家给我写一个java socket的简单例子
客户端的。
String ip = "172.31.1.130";
ip = "127.0.0.1";
int port = 10004;
try {
Socket socket = new Socket(ip, port);
socket.setSoTimeout(5539900);
java.io.OutputStream out = socket.getOutputStream();
byte[] date = "hello world".getBytes();
out.write(data);
out.flush();
socket.shutdownOutput();
byte[] buffer = new byte[1024];
int len = -1;
java.io.FileOutputStream fout = new java.io.FileOutputStream(
"d:/response.xml");
java.io.ByteArrayOutputStream bout = new java.io.ByteArrayOutputStream();
java.io.InputStream in = socket.getInputStream();
while ((len = in.read(buffer, 0, buffer.length)) > 0) {
bout.write(buffer, 0, len);
}
in.close();
bout.flush();
bout.close();
byte[] rdata = bout.toByteArray();
// System.out.println("leen = " + (rdata.length - 32));
System.out.println(new String(rdata));
fout.write(rdata);
fout.flush();
fout.close();
socket.close();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
服务器端的
ServerSocket ss = new ServerSocket(8090);
Socket socket=null;
BufferedReader in;
PrintWriter out;
while (true)
{
socket = ss.accept();
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream(),true);
String line = in.readLine();
out.println("you input is :" + line);
out.close();
in.close();
socket.close();
}
D. 用JAVA编写一个socket通信程序。
importjava.io.BufferedReader;
importjava.io.InputStreamReader;
importjava.net.ServerSocket;
importjava.net.Socket;
publicclassServer{
publicstaticvoidmain(String[]args){
ServerSocketss;
Sockets;
try{
ss=newServerSocket(8888);
s=ss.accept();
InputStreamReaderisr=newInputStreamReader(s.getInputStream());
BufferedReaderbr=newBufferedReader(isr);
System.out.println(br.readLine());
br.close();
isr.close();
}catch(Exceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}
}
importjava.io.PrintWriter;
importjava.net.Socket;
publicclassClient{
publicstaticvoidmain(String[]args){
try{
Sockets=newSocket("127.0.0.1",8888);
PrintWriterpw=newPrintWriter(s.getOutputStream());
pw.write("helloserver");
pw.flush();
pw.close();
}catch(Exceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}
}