51,396
社区成员




import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import com.net.csdn.socket.thread.SocketServerThread;
/**
*
* @author Administrator
*
*/
public class MyServer {
public static void conn() {
try {
ServerSocket ss = new ServerSocket(8080);
while (true) {
Socket so = ss.accept();
System.out.println("服务器新建立连接" + so.getInetAddress() + ":"
+ so.getPort());
new Thread(new SocketServerThread(so)).start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
conn();
}
}
import java.io.IOException;
import java.net.UnknownHostException;
import java.util.concurrent.ConcurrentLinkedQueue;
import com.net.csdn.socket.thread.SocketClientSendThread;
public class MyClient {
private static ConcurrentLinkedQueue<String> queue = new ConcurrentLinkedQueue<String>();
public static void main(String[] args) throws UnknownHostException,
IOException, InterruptedException {
SocketClientSendThread sc = new SocketClientSendThread(queue);
queue.add("0eof");
new Thread(sc).start();
// 多次发送数据
for (int i = 1; i < 100; i++) {
// Thread.sleep(2000);
queue.add(String.valueOf(i) + "eof");
}
}
}
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
public class SocketServerThread implements Runnable {
private Socket so;
public SocketServerThread(Socket socket) {
this.so = socket;
}
public void run() {
try {
while (true) {
byte[] by = new byte[5 * 1024];
DataInputStream ds = new DataInputStream(
so.getInputStream());
DataOutputStream os = new DataOutputStream(
so.getOutputStream());
String clientMsg = "";
boolean flag = false;
// 读取输入流
while (((clientMsg=ds.readUTF())!=null)) {
flag = true;
//clientMsg = new String(by);
int index = clientMsg.indexOf("eof");
if(index!=-1){
System.out.println("从客户端接受的数据"+clientMsg.substring(0,index));
os.writeUTF((clientMsg.substring(0, index) + "success eof")
);
os.flush();
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.concurrent.ConcurrentLinkedQueue;
public class SocketClientSendThread implements Runnable {
private ConcurrentLinkedQueue<String> queue;
public SocketClientSendThread(ConcurrentLinkedQueue<String> queue){
this.queue=queue;
}
public void run() {
try {
Socket so = new Socket("127.0.0.1", 8080,
InetAddress.getLocalHost(), 63313);
SocketClientReponseThread scr = new SocketClientReponseThread(so);
scr.start();
while (true) {
if (!queue.isEmpty()) {
DataOutputStream os = new DataOutputStream(
so.getOutputStream());
os.writeUTF(queue.poll());
os.flush();
}
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
import java.io.DataInputStream;
import java.io.IOException;
import java.net.Socket;
public class SocketClientReponseThread extends Thread{
private Socket so;
public SocketClientReponseThread(Socket socket) {
this.so = socket;
}
public void run() {
while (true) {
try {
DataInputStream is = new DataInputStream(
so.getInputStream());
String serverMsg;
while (((serverMsg=is.readUTF()) != null)) {
int index = serverMsg.indexOf("eof");
if (index != -1) {
System.out.println("从服务端接收到应答:"
+ serverMsg.substring(0, index));
break;
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}