import java.net.*;
import java.io.*;
public class XmlSocket
{
public static void main(String[] args){
try{
ServerSocket sarver = new ServerSocket(8080);
System.out.println("サーバを起動しました。");
while(true){
Socket socket = sarver.accept();
new ClientSoc(socket);
}
}catch (Exception e){
e.printStackTrace();
}
}
}
class ClientSoc extends Thread
{
Socket socket = null;
PrintWriter out = null;
BufferedReader in = null;
ClientSoc(Socket soc){
System.out.println("接続されました。" + soc);
socket = soc;
try{
in = new BufferedReader( new InputStreamReader(socket.getInputStream(), "UTF8"));
out = new PrintWriter( new OutputStreamWriter(socket.getOutputStream(), "UTF8"), true);
}catch (Exception e){
e.printStackTrace();
}
this.start();
}
public void run(){
try{
while (true){
String str = "";
int c = in.read();
if (c == -1){
break;
}
while (c != '\0'){
str += (char)c;
c = in.read();
}
System.out.println(str);
}
System.out.println("切断されました。" + socket);
in.close();
out.close();
socket.close();
}catch (Exception e){
e.printStackTrace();
}
}
}
|