package aptech.edu;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
public class TCPClient {
public static void main(String[] args) throws UnknownHostException, IOException {
// TODO Auto-generated method stub
try{
Socket myClient = new Socket("localhost", 9000);
OutputStream os = myClient.getOutputStream();
InputStream is = myClient.getInputStream();
for (char a = 'a'; a <= 'z'; a++) {
os.write((int) a);
System.out.println("Gui" + a);
char b = (char) is.read();
System.out.println("Nhan" + b);
}
myClient.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
package aptech.edu;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class TCPServer {
public static void main(String[] args) throws IOException{
try{
//Server mo socket cong 9000
ServerSocket myServer = new ServerSocket(9000);
System.out.println("Server started");
//Tao ra trang thai cho
Socket serverSocket = myServer.accept();
System.out.println("Connection from " + serverSocket);
//Socket co output stream
OutputStream os = serverSocket.getOutputStream();
InputStream is = serverSocket.getInputStream();
int ch = 0;
while(true){
ch = is.read();
if(ch == -1){
break;
}
os.write(ch);
System.out.println("Nhan: " + (char) ch);
}
myServer.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
0 nhận xét:
Post a Comment