当我尝试在Java中运行socket程序时,为什么会出现错误?
我正在使用jcreatorLE和JDK 1.6来运行我的程序.我不知道为什么在我尝试运行时出现错误.
有人可以向我解释一下原因吗? 这是服务器的代码: import java.io.*; import java.net.*; class ServidorTCP { // variable to wait for connections private static ServerSocket servidor = null; // Variable to process client connections private static Socket conexion = null; // To send data to the client private static DataOutputStream salida = null; // Read the client private static DataInputStream entrada = null; public static void main(String args[]) { // args [0] is the port number to be listened to int puerto = new Integer(args[0]).intValue(); // opening of the socket try { // Port where the client requests pending servidor = new ServerSocket(puerto); System.out.println("Servidor TCP iniciado..."); boolean ejecutar = true; // It is starting to respond to requests while (ejecutar) { System.out.println("nWaiting for Connections..."); conexion = servidor.accept(); // Connection is expected // The connection was established with the client entrada = new DataInputStream(conexion.getInputStream()); salida = new DataOutputStream(conexion.getOutputStream()); System.out.println("nConexion recibida...n"); String inicio = entrada.readUTF(); System.out.println("Reception Date: "+new java.util.Date()); System.out.println("From: "+(conexion.getInetAddress()).toString()); System.out.println("received: "+inicio); salida.writeUTF(inicio.toUpperCase()); entrada.close(); salida.close(); conexion.close(); } // Close the socket System.out.println("ntransmission completed...n"); servidor.close(); } catch (Exception e) { e.printStackTrace(); } } } 这是客户端套接字程序的代码: import java.io.*; import java.net.*; class ClienteTCP { private static Socket cliente = null; private static DataInputStream entrada = null; private static DataOutputStream salida = null; public static void main(String args[]) { int len = new Integer(args.length); String cadena = ""; System.out.println("nNumero de arg: "+len); for (int i=2; i<len;i++) { cadena = cadena + args[i]; cadena = cadena + " "; } System.out.println("nLa Cadena: "+cadena); int puerto = new Integer(args[1]).intValue(); try { cliente = new Socket(args[0],puerto); entrada = new DataInputStream(cliente.getInputStream()); salida = new DataOutputStream(cliente.getOutputStream()); //SENDING INFORMATION DATA System.out.println("nEnviando datos al servidor: "+cadena); salida.writeUTF(cadena); //Recibiendo la información System.out.println("Recibido: "+entrada.readUTF()); entrada.close(); salida.close(); //Cierre del socket cliente.close(); System.out.println("nConexion terminada...n"); } catch (Exception e) { e.printStackTrace(); } } } 这是运行它的结果: [Loaded java.lang.Object from shared objects file] [Loaded java.io.Serializable from shared objects file] [Loaded java.lang.Comparable from shared objects file] [Loaded java.lang.CharSequence from shared objects file] [Loaded java.lang.String from shared objects file] [Loaded java.lang.reflect.GenericDeclaration from shared objects file] [Loaded java.lang.reflect.Type from shared objects file] [Loaded java.lang.reflect.AnnotatedElement from shared objects file] [Loaded java.lang.Class from shared objects file] [Loaded java.lang.Cloneable from shared objects file] [Loaded java.lang.ClassLoader from shared objects file] [Loaded java.lang.System from shared objects file] [Loaded java.lang.Throwable from shared objects file] [Loaded java.lang.Error from shared objects file] [Loaded java.lang.ThreadDeath from shared objects file] [Loaded java.lang.Exception from shared objects file] [Loaded java.lang.RuntimeException from shared objects file] [Loaded java.security.ProtectionDomain from shared objects file] [Loaded java.security.AccessControlContext from shared objects file] [Loaded java.lang.ClassNotFoundException from shared objects file] [Loaded java.lang.LinkageError from shared objects file] [Loaded java.lang.NoClassDefFoundError from shared objects file] [Loaded java.lang.ClassCastException from shared objects file] [Loaded java.lang.ArrayStoreException from shared objects file] [Loaded java.lang.VirtualMachineError from shared objects file] [Loaded java.lang.OutOfMemoryError from shared objects file] [Loaded java.lang.StackOverflowError from shared objects file] [Loaded java.lang.IllegalMonitorStateException from shared objects file] [Loaded java.lang.ref.Reference from shared objects file] [Loaded java.lang.ref.SoftReference from shared objects file] [Loaded java.lang.ref.WeakReference from shared objects file] [Loaded java.lang.ref.FinalReference from shared objects file] [Loaded java.lang.ref.PhantomReference from shared objects file] [Loaded java.lang.ref.Finalizer from shared objects file] [Loaded java.lang.Runnable from shared objects file] [Loaded java.lang.Thread from shared objects file] [Loaded java.lang.Thread$UncaughtExceptionHandler from shared objects file] [Loaded java.lang.ThreadGroup from shared objects file] [Loaded java.util.Dictionary from shared objects file] [Loaded java.util.Map from shared objects file] [Loaded java.util.Hashtable from shared objects file] [Loaded java.util.Properties from shared objects file] [Loaded java.lang.reflect.AccessibleObject from shared objects file] [Loaded java.lang.reflect.Member from shared objects file] [Loaded java.lang.reflect.Field from shared objects file] [Loaded java.lang.reflect.Method from shared objects file] [Loaded java.lang.reflect.Constructor from shared objects file] [Loaded sun.reflect.MagicAccessorImpl from shared objects file] [Loaded sun.reflect.MethodAccessor from shared objects file] [Loaded sun.reflect.MethodAccessorImpl from shared objects file] [Loaded sun.reflect.ConstructorAccessor from shared objects file] [Loaded sun.reflect.ConstructorAccessorImpl from shared objects file] [Loaded sun.reflect.DelegatingClassLoader from shared objects file] [Loaded sun.reflect.ConstantPool from shared objects file] [Loaded sun.reflect.FieldAccessor from shared objects file] [Loaded sun.reflect.FieldAccessorImpl from shared objects file] [Loaded sun.reflect.UnsafeFieldAccessorImpl from shared objects file] [Loaded sun.reflect.UnsafeStaticFieldAccessorImpl from shared objects file] [Loaded java.lang.Iterable from shared objects file] [Loaded java.util.Collection from shared objects file] [Loaded java.util.AbstractCollection from shared objects file] [Loaded java.util.List from shared objects file] [Loaded java.util.AbstractList from shared objects file] [Loaded java.util.RandomAccess from shared objects file] [Loaded java.util.Vector from shared objects file] [Loaded java.lang.Appendable from shared objects file] [Loaded java.lang.AbstractStringBuilder from shared objects file] [Loaded java.lang.StringBuffer from shared objects file] [Loaded java.lang.StackTraceElement from shared objects file] [Loaded java.nio.Buffer from shared objects file] [Loaded sun.misc.AtomicLong from shared objects file] [Loaded sun.misc.AtomicLongCSImpl from shared objects file] [Loaded java.lang.Boolean from shared objects file] [Loaded java.lang.Character from shared objects file] [Loaded java.lang.Number from shared objects file] [Loaded java.lang.Float from shared objects file] [Loaded java.lang.Double from shared objects file] [Loaded java.lang.Byte from shared objects file] [Loaded java.lang.Short from shared objects file] [Loaded java.lang.Integer from shared objects file] [Loaded java.lang.Long from shared objects file] [Loaded java.io.ObjectStreamField from shared objects file] [Loaded java.util.Comparator from shared objects file] [Loaded java.lang.String$CaseInsensitiveComparator from shared objects file] [Loaded java.security.Guard from shared objects file] [Loaded java.security.Permission from shared objects file] [Loaded java.security.BasicPermission from shared objects file] [Loaded java.lang.RuntimePermission from shared objects file] [Loaded java.util.AbstractMap from shared objects file] [Loaded sun.misc.SoftCache from shared objects file] [Loaded java.lang.ref.ReferenceQueue from shared objects file] [Loaded java.lang.ref.ReferenceQueue$Null from shared objects file] [Loaded java.lang.ref.ReferenceQueue$Lock from shared objects file] [Loaded java.util.HashMap from shared objects file] [Loaded java.lang.annotation.Annotation from shared objects file] [Loaded java.util.Map$Entry from shared objects file] [Loaded java.util.HashMap$Entry from shared objects file] [Loaded java.security.AccessController from shared objects file] [Loaded java.lang.reflect.ReflectPermission from shared objects file] [Loaded java.security.PrivilegedAction from shared objects file] [Loaded sun.reflect.ReflectionFactory$GetReflectionFactoryAction from shared objects file] [Loaded java.util.Stack from shared objects file] [Loaded sun.reflect.ReflectionFactory from shared objects file] [Loaded java.lang.ref.Reference$Lock from shared objects file] [Loaded java.lang.ref.Reference$ReferenceHandler from shared objects file] [Loaded java.lang.ref.Finalizer$FinalizerThread from shared objects file] [Loaded java.util.Enumeration from shared objects file] [Loaded java.util.Hashtable$EmptyEnumerator from shared objects file] [Loaded java.util.Iterator from shared objects file] [Loaded java.util.Hashtable$EmptyIterator from shared objects file] [Loaded java.util.Hashtable$Entry from shared objects file] [Loaded sun.misc.Version from shared objects file] [Loaded java.lang.Runtime from shared objects file] [Loaded sun.reflect.Reflection from shared objects file] [Loaded java.util.Collections from shared objects file] [Loaded java.util.Set from shared objects file] [Loaded java.util.AbstractSet from shared objects file] [Loaded java.util.Collections$EmptySet from shared objects file] [Loaded java.util.Collections$EmptyList from shared objects file] [Loaded java.util.Collections$EmptyMap from shared objects file] [Loaded java.util.Collections$ReverseComparator from shared objects file] [Loaded java.util.Collections$SynchronizedMap from shared objects file] [Loaded java.io.File from shared objects file] [Loaded java.io.FileSystem from shared objects file] [Loaded java.io.Win32FileSystem from shared objects file] [Loaded java.io.WinNTFileSystem from shared objects file] [Loaded java.io.ExpiringCache from shared objects file] [Loaded java.util.LinkedHashMap from shared objects file] [Loaded java.io.ExpiringCache$1 from shared objects file] [Loaded java.util.LinkedHashMap$Entry from shared objects file] [Loaded sun.security.action.GetPropertyAction from shared objects file] [Loaded java.lang.StringBuilder from shared objects file] [Loaded java.util.Arrays from shared objects file] [Loaded java.lang.Math from shared objects file] [Loaded sun.misc.JavaIODeleteOnExitAccess from shared objects file] [Loaded java.io.File$1 from shared objects file] [Loaded sun.misc.SharedSecrets from shared objects file] [Loaded sun.misc.Unsafe from shared objects file] [Loaded java.lang.IncompatibleClassChangeError from shared objects file] [Loaded java.lang.NoSuchMethodError from shared objects file] [Loaded sun.jkernel.DownloadManager from shared objects file] [Loaded java.lang.ThreadLocal from shared objects file] [Loaded sun.jkernel.DownloadManager$1 from shared objects file] [Loaded java.util.concurrent.atomic.AtomicInteger from shared objects file] [Loaded java.lang.Class$3 from shared objects file] [Loaded java.lang.reflect.Modifier from shared objects file] [Loaded sun.reflect.LangReflectAccess from shared objects file] [Loaded java.lang.reflect.ReflectAccess from shared objects file] [Loaded sun.jkernel.DownloadManager$2 from shared objects file] [Loaded java.lang.ClassLoader$3 from shared objects file] [Loaded java.io.ExpiringCache$Entry from shared objects file] [Loaded java.lang.ClassLoader$NativeLibrary from shared objects file] [Loaded java.io.Closeable from shared objects file] [Loaded java.io.InputStream from shared objects file] [Loaded java.io.FileInputStream from shared objects file] [Loaded java.io.FileDescriptor from shared objects file] [Loaded java.io.Flushable from shared objects file] [Loaded java.io.OutputStream from shared objects file] [Loaded java.io.FileOutputStream from shared objects file] [Loaded java.io.FilterInputStream from shared objects file] [Loaded java.io.BufferedInputStream from shared objects file] [Loaded java.util.concurrent.atomic.AtomicReferenceFieldUpdater from shared objects file] [Loaded java.util.concurrent.atomic.AtomicReferenceFieldUpdater$AtomicReferenceFieldUpdaterImpl from shared objects file] [Loaded sun.reflect.misc.ReflectUtil from shared objects file] [Loaded java.io.FilterOutputStream from shared objects file] [Loaded java.io.PrintStream from shared objects file] [Loaded java.io.BufferedOutputStream from shared objects file] [Loaded java.io.Writer from shared objects file] [Loaded java.io.OutputStreamWriter from shared objects file] [Loaded sun.nio.cs.StreamEncoder from shared objects file] [Loaded java.nio.charset.Charset from shared objects file] [Loaded java.nio.charset.spi.CharsetProvider from shared objects file] [Loaded sun.nio.cs.FastCharsetProvider from shared objects file] [Loaded sun.nio.cs.StandardCharsets from shared objects file] [Loaded sun.util.PreHashedMap from shared objects file] [Loaded sun.nio.cs.StandardCharsets$Aliases from shared objects file] [Loaded sun.nio.cs.StandardCharsets$Classes from shared objects file] [Loaded sun.nio.cs.StandardCharsets$Cache from shared objects file] [Loaded sun.nio.cs.HistoricallyNamedCharset from shared objects file] [Loaded sun.nio.cs.MS1252 from shared objects file] [Loaded java.lang.Class$1 from shared objects file] [Loaded sun.reflect.ReflectionFactory$1 from shared objects file] [Loaded sun.reflect.NativeConstructorAccessorImpl from shared objects file] [Loaded sun.reflect.DelegatingConstructorAccessorImpl from shared objects file] [Loaded sun.misc.VM from shared objects file] [Loaded java.nio.charset.CharsetEncoder from shared objects file] [Loaded sun.nio.cs.SingleByteEncoder from shared objects file] [Loaded sun.nio.cs.MS1252$Encoder from shared objects file] [Loaded java.nio.charset.CodingErrorAction from shared objects file] [Loaded java.nio.charset.CharsetDecoder from shared objects file] [Loaded sun.nio.cs.SingleByteDecoder from shared objects file] [Loaded sun.nio.cs.MS1252$Decoder from shared objects file] [Loaded java.nio.ByteBuffer from shared objects file] [Loaded java.nio.HeapByteBuffer from shared objects file] [Loaded java.nio.Bits from shared objects file] [Loaded java.nio.ByteOrder from shared objects file] [Loaded java.lang.Readable from shared objects file] [Loaded java.nio.CharBuffer from shared objects file] [Loaded java.nio.HeapCharBuffer from shared objects file] [Loaded java.nio.charset.CoderResult from shared objects file] [Loaded java.nio.charset.CoderResult$Cache from shared objects file] [Loaded java.nio.charset.CoderResult$1 from shared objects file] [Loaded java.nio.charset.CoderResult$2 from shared objects file] [Loaded sun.nio.cs.Surrogate$Parser from shared objects file] [Loaded sun.nio.cs.Surrogate from shared objects file] [Loaded java.io.BufferedWriter from shared objects file] [Loaded java.lang.Terminator from shared objects file] [Loaded sun.misc.SignalHandler from shared objects file] [Loaded java.lang.Terminator$1 from shared objects file] [Loaded sun.misc.Signal from shared objects file] [Loaded sun.misc.NativeSignalHandler from shared objects file] [Loaded java.io.Console from shared objects file] [Loaded sun.misc.JavaIOAccess from shared objects file] [Loaded java.io.Console$1 from shared objects file] [Loaded java.io.Console$1$1 from shared objects file] [Loaded java.lang.Shutdown from shared objects file] [Loaded java.util.ArrayList from shared objects file] [Loaded java.lang.Shutdown$Lock from shared objects file] [Loaded java.lang.ApplicationShutdownHooks from shared objects file] [Loaded java.util.IdentityHashMap from shared objects file] [Loaded sun.misc.OSEnvironment from shared objects file] [Loaded sun.io.Win32ErrorMode from shared objects file] [Loaded sun.misc.JavaLangAccess from shared objects file] [Loaded java.lang.System$2 from shared objects file] [Loaded java.lang.NullPointerException from shared objects file] [Loaded java.lang.ArithmeticException from shared objects file] [Loaded java.lang.Compiler from shared objects file] [Loaded java.lang.Compiler$1 from shared objects file] [Loaded sun.misc.Launcher from shared objects file] [Loaded java.net.URLStreamHandlerFactory from shared objects file] [Loaded sun.misc.Launcher$Factory from shared objects file] [Loaded java.security.SecureClassLoader from shared objects file] [Loaded java.net.URLClassLoader from shared objects file] [Loaded sun.misc.Launcher$ExtClassLoader from shared objects file] [Loaded sun.security.util.Debug from shared objects file] [Loaded sun.misc.JavaNetAccess from shared objects file] [Loaded java.net.URLClassLoader$7 from shared objects file] [Loaded java.util.StringTokenizer from shared objects file] [Loaded java.security.PrivilegedExceptionAction from shared objects file] [Loaded sun.misc.Launcher$ExtClassLoader$1 from shared objects file] [Loaded sun.misc.MetaIndex from shared objects file] [Loaded java.io.Reader from shared objects file] [Loaded java.io.BufferedReader from shared objects file] [Loaded java.io.InputStreamReader from shared objects file] [Loaded java.io.FileReader from shared objects file] [Loaded sun.nio.cs.StreamDecoder from shared objects file] [Loaded java.lang.reflect.Array from shared objects file] [Loaded java.util.Locale from shared objects file] [Loaded java.util.concurrent.ConcurrentMap from shared objects file] [Loaded java.util.concurrent.ConcurrentHashMap from shared objects file] [Loaded java.util.concurrent.locks.Lock from shared objects file] [Loaded java.util.concurrent.locks.ReentrantLock from shared objects file] [Loaded java.util.concurrent.ConcurrentHashMap$Segment from shared objects file] [Loaded java.util.concurrent.locks.AbstractOwnableSynchronizer from shared objects file] [Loaded java.util.concurrent.locks.AbstractQueuedSynchronizer from shared objects file] [Loaded java.util.concurrent.locks.ReentrantLock$Sync from shared objects file] [Loaded java.util.concurrent.locks.ReentrantLock$NonfairSync from shared objects file] [Loaded java.util.concurrent.locks.AbstractQueuedSynchronizer$Node from shared objects file] [Loaded java.util.concurrent.ConcurrentHashMap$HashEntry from shared objects file] [Loaded java.lang.CharacterDataLatin1 from shared objects file] [Loaded java.io.ObjectStreamClass from shared objects file] [Loaded sun.net.www.ParseUtil from shared objects file] [Loaded java.util.BitSet from shared objects file] [Loaded java.net.URL from shared objects file] [Loaded java.net.Parts from shared objects file] [Loaded java.net.URLStreamHandler from shared objects file] [Loaded sun.net.www.protocol.file.Handler from shared objects file] [Loaded java.util.HashSet from shared objects file] [Loaded sun.misc.URLClassPath from shared objects file] [Loaded sun.net.www.protocol.jar.Handler from shared objects file] [Loaded sun.misc.Launcher$AppClassLoader from shared objects file] [Loaded sun.misc.Launcher$AppClassLoader$1 from shared objects file] [Loaded java.lang.SystemClassLoaderAction from shared objects file] [Loaded java.lang.StringCoding from shared objects file] [Loaded java.lang.ThreadLocal$ThreadLocalMap from shared objects file] [Loaded java.lang.ThreadLocal$ThreadLocalMap$Entry from shared objects file] [Loaded java.lang.StringCoding$StringDecoder from shared objects file] [Loaded java.net.URLClassLoader$1 from shared objects file] [Loaded sun.misc.URLClassPath$3 from shared objects file] [Loaded sun.misc.URLClassPath$Loader from shared objects file] [Loaded sun.misc.URLClassPath$JarLoader from shared objects file] [Loaded java.security.PrivilegedActionException from shared objects file] [Loaded sun.misc.URLClassPath$FileLoader from shared objects file] [Loaded sun.misc.Resource from shared objects file] [Loaded sun.misc.URLClassPath$FileLoader$1 from shared objects file] [Loaded sun.nio.ByteBuffered from shared objects file] [Loaded java.security.CodeSource from shared objects file] [Loaded java.security.PermissionCollection from shared objects file] [Loaded java.security.Permissions from shared objects file] [Loaded java.net.URLConnection from shared objects file] [Loaded sun.net.www.URLConnection from shared objects file] [Loaded sun.net.www.protocol.file.FileURLConnection from shared objects file] [Loaded java.net.ContentHandler from shared objects file] [Loaded java.net.UnknownContentHandler from shared objects file] [Loaded sun.net.www.MessageHeader from shared objects file] [Loaded java.io.FilePermission from shared objects file] [Loaded java.io.FilePermission$1 from shared objects file] [Loaded java.security.Policy from shared objects file] [Loaded sun.security.provider.PolicyFile from shared objects file] [Loaded java.security.Policy$UnsupportedEmptyCollection from shared objects file] [Loaded java.io.FilePermissionCollection from shared objects file] [Loaded java.security.AllPermission from shared objects file] [Loaded java.security.UnresolvedPermission from shared objects file] [Loaded java.security.BasicPermissionCollection from shared objects file] [Loaded java.security.Principal from shared objects file] [Loaded java.security.cert.Certificate from shared objects file] [Loaded ServidorTCP from file:/C:/Users/administrador/Documents/] [Loaded java.lang.IndexOutOfBoundsException from shared objects file] [Loaded java.lang.ArrayIndexOutOfBoundsException from shared objects file] Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 at ServidorTCP.main(ServidorTCP.java:21) [Loaded java.util.AbstractList$Itr from shared objects file] [Loaded java.util.IdentityHashMap$KeySet from shared objects file] [Loaded java.util.IdentityHashMap$IdentityHashMapIterator from shared objects file] [Loaded java.util.IdentityHashMap$KeyIterator from shared objects file] [Loaded java.io.DeleteOnExitHook from shared objects file] [Loaded java.util.LinkedHashSet from shared objects file] [Loaded java.util.HashMap$KeySet from shared objects file] [Loaded java.util.LinkedHashMap$LinkedHashIterator from shared objects file] [Loaded java.util.LinkedHashMap$KeyIterator from shared objects file] Process completed. 解决方法
您必须在命令行指定端口号.
了解如何阅读日志总是有用的. 看到.在您的日志中,您有一堆类名,但只有一个与您的代码相关: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 at ServidorTCP.main(ServidorTCP.java:21) 其余的都是关于java.lang,java.util,java.io等,你没有编码的类. 如果您查看该错误行,它会说: java.lang.ArrayIndexOutOfBoundsException: 这意味着您试图从其边界外访问数组.也就是说,如果数组大小为5,则可能尝试访问第6个元素.这是非法的,这是一个例外(更正式的是RuntimeException意味着你有编程错误) 然后日志会在发生的地方添加信息. at ServidorTCP.main(ServidorTCP.java:21) 在ServidorTCP类中(在java文件ServidorTCP第21行). 您可以在该行查看该文件,这是它显示的内容: 20: // args [0] is the port number to be heard 21: int puerto = new Integer(args[0]).intValue(); 也就是说,变量“puerto”的值将是使用args [0]的内容创建的新Integer.这是args数组的第一个元素. 该数组在main方法中定义: public static void main(String args[]){ 并自动填充命令行参数. 因此,如果在没有提供参数的情况下运行程序,则在数组args中搜索索引0将失败. 我不知道在jcreator中你可以添加命令行参数的确切位置,但我很确定在“运行”菜单中的某个地方,或者在“运行”配置或类似的东西中,应该有类似“程序参数”的内容“您可以使用端口号填充该值,请尝试使用8085. 要么 您还可以更改服务器的第21行并将其初始化为: //int puerto = new Integer(args[0]).intValue(); int puerto = 8085; // por mis pistolas :) 并看到它运行. Espero que esto te ayude. 洲! (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |