Instructions for Java TCP Socket programming with server/client examples following standard Java Socket practices
This skill provides guidance for Java TCP Socket programming projects, focusing on server and client implementation following standard Java Socket practices.
Use this skill when working on Java TCP Socket programming projects. It ensures generated code follows Java Socket API best practices and conventions.
When generating code for this project:
1. **Language Priority**: Always prioritize Java code generation for Socket-related tasks.
2. **Standard Practices**: Follow standard Java Socket programming practices:
- Use `java.net.Socket` for client connections
- Use `java.net.ServerSocket` for server implementations
- Properly handle `IOException` and other socket-related exceptions
- Use try-with-resources for automatic resource management
- Implement proper connection closing and cleanup
3. **Server Implementation Guidelines**:
- Create ServerSocket bound to appropriate port
- Use `accept()` method to listen for client connections
- Handle multiple clients using threads or ExecutorService
- Implement proper shutdown mechanisms
- Add logging for connection events
4. **Client Implementation Guidelines**:
- Establish Socket connection to server host and port
- Use BufferedReader/PrintWriter or DataInputStream/DataOutputStream for communication
- Implement proper error handling and connection retry logic
- Close resources in finally blocks or use try-with-resources
5. **Communication Patterns**:
- Use appropriate stream classes (InputStream/OutputStream) for data exchange
- Consider using ObjectInputStream/ObjectOutputStream for object serialization if needed
- Implement message protocols or framing when necessary
- Handle partial reads and writes correctly
6. **Error Handling**:
- Catch and handle SocketException, IOException appropriately
- Implement timeout mechanisms using `setSoTimeout()`
- Provide meaningful error messages
- Log exceptions with stack traces for debugging
7. **Code Structure**:
- Separate concerns (server logic, client logic, message handling)
- Use meaningful class and method names
- Add comments for complex logic
- Follow Java naming conventions (camelCase for methods/variables, PascalCase for classes)
When asked to create a TCP server, generate code that:
When asked to create a TCP client, generate code that:
Leave a review
No reviews yet. Be the first to review this skill!
# Download SKILL.md from killerskills.ai/api/skills/java-tcp-socket-programming-guide/raw