A. TCP Transmission
A) TCP split-client and server-side
I. The client's corresponding object is the socket
Ii. server-side corresponding object is ServerSocket
(b) By looking at the socket object, it is found that when the object is established, it is possible to connect to the specified host, because TCP is connection-oriented, so when the socket service is established, there must be a service-side presence, and the connection is successful, the channel is formed after the data transfer.
c) Steps:
I. Create a socket service and specify the host and port to which you want to connect.
Ii. in order to send data, the output stream in the socket stream should be obtained
Iii. establish a service-side socket service, ServerSocket () and listen for a port.
Iv. Get connected client objects, through the ServerSocket accept method, no connection will wait, so this method blocking the
V. If the client sends the data, the server uses the corresponding client object and gets the read stream to the client object to read the data sent over. and print it in the console.
Vi. Closing the service side
package Com.jwd.black.cs;
import java.io.*;
import java.net.*;
Publicclass TCPServer {
publicstaticvoid Main (String[]args)throws exception{
ServerSocket ss =new serversocket (10003);
Sockets =ss.accept ();
Inputstreamin =s.getinputstream ();
byte []BUF =newbyte[1024];
intlen =in.read (BUF);
System. out. println (new String (Buf,0,len));
S.close ()//Close Client
}
}
package Com.jwd.black.cs;
import java.io.*;
import java.net.*;
Publicclass TcpClient {
publicstaticvoid Main (String[]args)throws Exception {
Sockets =new Socket ("192.168.1.248", 100003);
Outputstreamout=s.getoutputstream ();
Out.write ("Tct lai le". GetBytes ());
S.close ();
}
}
Two. tcp-Upload picture
A) client
I. Service Endpoints
II. Read the client already has picture data
Iii. sending data to the server via the socket output stream
Iv. reading server-side feedback
V. Close
b) The sample code is as follows
package com.jwd.black.picture;
import java.net.*;
import java.io.*;
Publicclasspicclient {
publicstaticvoid Main (String[]args)throws Exception {
TODO auto-generated Method stub
Sockets =new Socket ("192.168.1.117", 10007);
fileinputstreamfis=Newfileinputstream ("1.bmp");
Outputstreamout=s.getoutputstream ();
byte []BUF =newbyte[1024];
intlen = 0;
while ((Len=fis.read (BUF))!=-1) {
Out.write (Buf,0,len);
}
Tell the server that the data has been written out.
S.shutdowninput ();
Inputstreamin =s.getinputstream ();
byte []bufin =newbyte[1024];
intnum =in.read (bufin);
System. out. Print (new String (Bufin,0,num));
Fis.close ();
S.close ();
}
}
package com.jwd.black.picture;
import java.net.*;
import java.io.*;
Publicclass Picserver {
publicstaticvoid Main (String[]args)throws Exception {
TODO auto-generated Method stub
SERVERSOCKETSS =new serversocket (10007);
Sockets =ss.accept ();
Inputstreamin =s.getinputstream ();
fileoutputstreamfos=Newfileoutputstream ("server.bmp");
byte []BUF =newbyte[1024];
intlen = 0;
while ((Len =in.read (BUF))!=-1) {
Fos.write (buf, 0,len);
}
Outputstreamout=s.getoutputstream ();
Out.write ("Upload succeeded". GetBytes ());
Fos.close ();
S.close ();
Ss.close ();
}
}
c The service side has a limitation, when a client connection, by the server to obtain, the server to perform the specific process, then B client connection, only waiting
D because the server has not finished processing a client's flag open request, and loop back to perform the next accept method, so temporarily get no B client object.
E in order to allow multiple clients concurrent access to the server, then the best service side is to encapsulate each tedious user into a separate thread, so that multiple client requests can be made simultaneously.
f) How to define a thread
I just need to be clear about the code that each client executes on the server. Save the code in the Run method.