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();
}
}
}