标签:
Socket多线程编程 serversocket服务器多线程编程
服务器代码
package com.lotus.server;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
public class SocketServer {
List<Socket> list;
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
SocketServer socketServer=new SocketServer();
socketServer.startServer();
if(Thread.activeCount()>1){
Thread.yield();
}
}
public void startServer(){
ServerSocket serverSocket=null;
try {
list=new ArrayList<Socket>();
serverSocket=new ServerSocket(9898);
new MeldSocket(serverSocket).start();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
class MeldSocket extends Thread{
private ServerSocket serverSocket;
public MeldSocket(ServerSocket serverSocket){
this.serverSocket=serverSocket;
}
@Override
public void run() {
// TODO Auto-generated method stub
Socket socket;
while(true){
try {
socket = serverSocket.accept();
new ReaderThread(socket).start();
new WriterThread(socket).start();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
class WriterThread extends Thread{
private Socket socket=null;
// BufferedReader inputReader=null;
BufferedWriter writer=null;
public WriterThread(Socket socket){
this.socket=socket;
}
@Override
public void run() {
// TODO Auto-generated method stub
try {
System.out.println("开始输入");
writer=new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
// inputReader=new BufferedReader(new InputStreamReader(System.in));
new Timer().schedule(new TimerTask() {
@Override
public void run() {
// TODO Auto-generated method stub
try {
writer.write("老子在做测试\n");
writer.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}, 1000, 10000);
// String inputContent;
// while((inputContent=inputReader.readLine())!="byte"){
// System.out.println(inputContent);
//
// }
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class ReaderThread extends Thread{
private Socket socket=null;
BufferedInputStream reader=null;
public ReaderThread(Socket socket){
this.socket=socket;
}
@Override
public void run() {
// TODO Auto-generated method stub
try {
reader=new BufferedInputStream(socket.getInputStream());
System.out.println("接收到输入的信息");
int len=0;
byte[] bty=new byte[1024];
while ((len = reader.read(bty)) != -1) {
String inputContent=new String(bty,0,len);
System.out.println(inputContent);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
reader.close();
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
客户端代码
package com.lotus.pushlangconnect;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;
import javax.sound.sampled.AudioFormat.Encoding;
public class SocketClient {
public SocketClient() {
}
public static void main(String[] args) throws IOException {
SocketClient client=new SocketClient();
client.start();
}
public void start() throws IOException{
Socket socket=null;
try {
socket=new Socket("localhost", 9898);
new ReaderThread(socket).start();
new WriterThread(socket).start();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
class WriterThread extends Thread{
private Socket socket=null;
BufferedReader inputReader=null;
BufferedWriter writer=null;
public WriterThread(Socket socket){
this.socket=socket;
}
@Override
public void run() {
// TODO Auto-generated method stub
try {
System.out.println("开始输入");
writer=new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
inputReader=new BufferedReader(new InputStreamReader(System.in));
String inputContent;
while((inputContent=inputReader.readLine())!="byte"){
// System.out.println(inputContent);
writer.write(inputContent);
writer.flush();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
writer.close();
inputReader.close();
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
class ReaderThread extends Thread{
private Socket socket=null;
BufferedReader reader=null;
public ReaderThread(Socket socket){
this.socket=socket;
}
@Override
public void run() {
// TODO Auto-generated method stub
try {
reader=new BufferedReader(new InputStreamReader(socket.getInputStream()));
String respsonseContent;
while((respsonseContent=reader.readLine())!=null){
System.out.println(respsonseContent);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
reader.close();
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
Socket多线程编程 serversocket服务器多线程编程
标签:
原文地址:http://www.cnblogs.com/lotus-emperor/p/4761386.html